title: Long to wide on multiple columns by data.table
body: <p>I have a question about <code>data.table</code>'s <code>melt</code> and <code>dcast</code> for multiple columns. I browsed on StackOverFlow but many similar posts are not what I want. I will explain below.</p>
  
  <p>First, <code>data</code> is about causes of problems and the value amount. Here is my part of <code>data</code>:  </p>
  
  <pre><code>ID   Type    Problem1    Value1     Problem2    Value2    Problem3    Value3
  1    A       X           500        Y           1000      Z           400
  2    A       X           600        Z           700       
  3    B       Y           700        Z           100
  4    B       W           200        V           200
  5    C       Z           500        V           500       
  6    C       X           1000       W           100       V           900
  </code></pre>
  
  <p>Second, <code>ID</code> is unique. <code>Type</code> contains three(<code>A</code>, <code>B</code>, and <code>C</code>). There are <strong>5</strong> problems.  </p>
  
  <p>Take <code>ID == 1</code> as an example. It is <code>Type A</code> and it contains 3 problems(<code>X</code>, <code>Y</code>, and <code>Z</code>). Its <code>Problem X</code> has <code>Value 500</code>, <code>Problem Y</code> has <code>Value 1000</code>, <code>Problem Z</code> has <code>Value 400</code>. Take <code>ID == 5</code> as an example. It is <code>Type C</code> and contains 2 problems(<code>Z</code> and <code>V</code>). Its <code>Problem Z</code> has <code>Value 500</code> and <code>Problem V</code> has <code>Value 500</code>.  </p>
  
  <p>Third, column <code>ID</code>, <code>Type</code>, <code>Problem1</code>, <code>Problem2</code>, and <code>Problem3</code> are <code>character</code>. <code>Value1</code>, <code>Value2</code>, and <code>Value3</code> are <code>numeric</code>.   </p>
  
  <p>The result what I want is:  </p>
  
  <pre><code>Type    X     Y     Z     W     V
  A       1100  1000  1100  0     0   
  B       0     700   100   200   200
  C       1000  0     500   100   1400  
  </code></pre>
  
  <p>I don't know how to explain here properly. I want to group the <code>Type</code> and then summation each problem's vlaue. I think this is about <em>long to wide</em>. I found reference <a href="https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format">here</a> and <a href="https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reshape.html" rel="nofollow noreferrer">here</a>. Second one may be useful. However, I have no idea where to begin. Any suggestions?</p>
  
  <pre><code># data
  dt &lt;- fread("
  ID   Type    Problem1    Value1     Problem2    Value2    Problem3    Value3
  1    A       X           500        Y           1000      Z           400
  2    A       X           600        Z           700       
  3    B       Y           700        Z           100
  4    B       W           200        V           200
  5    C       Z           500        V           500       
  6    C       X           1000       W           100       V           900", fill = T)    
  </code></pre>