title: Reshaping from long to wide for characters instead of values and manipulation in R
body: <p>I have a subdata as below. I would like to do some manipulations in 3 steps:</p>
  <ol>
  <li>reshape it to long format so that for each &quot;id&quot; I see the &quot;m&quot; columns.</li>
  <li>for each id only keeping one of the repetitions, e.g id 101 has two &quot;15 ag&quot;, I would like to keep only one. Only one even I see many repetitions.</li>
  <li>assign the values in column &quot;m&quot; to some scores, as: &quot;15 ag&quot; assign to 0, &quot;12 cer&quot; assign to 1,&quot;18 di&quot; assign to 6,&quot;11 dem&quot; assign to 2,&quot;25 dia&quot; assign to 0.</li>
  <li>then I sum all the scores up in a new column called &quot;sum&quot;.
  In the end, I will have for each id the &quot;sum&quot;.</li>
  </ol>
  <p>Any help for this is appreciated.</p>
  <pre><code>d &lt;- structure(list(id = c(101, 101, 101, 101, 102, 102, 102, 103, 
                             103, 103, 103, 104), m = c(&quot;15 ag&quot;, &quot;15 ag&quot;, NA, &quot;12 cer&quot;, NA, 
                            &quot;18 di&quot;, &quot;12 cer&quot;, &quot;11 dem&quot;, &quot;11 dem&quot;, NA, &quot;12 cer&quot;, &quot;25 dia&quot;
                             )), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;),row.names =c(NA,-12L))
  </code></pre>