title: Transpose data by groups in R
body: <p>I have data in the following structure:</p>
  
  <pre><code>x &lt;- read.table(header=T, text="
  X Y D S
  a e 1 10
  a e 2 20
  a f 1 50
  b c 1 40
  b c 2 30
  b c 3 60
  b d 1 10 
  b d 2 20")
  </code></pre>
  
  <p>And I want to get the following result:</p>
  
  <pre><code>X Y   1   2   3
  a e  10  20
  a f  50
  b c  40  30  60
  b d  10  20
  </code></pre>
  
  <p>For every combination of columns <code>X</code> and <code>Y</code> I would like to transpose data in column <code>S</code> by order in column <code>D</code>. </p>
  
  <p>I thought <code>xtabs()</code> will work, but I don't think so, my best version is:</p>
  
  <pre><code>xtabs(formula=S~Y+D,data=x)
  </code></pre>
  
  <p>With result:</p>
  
  <pre><code>   D
  Y    1  2  3
    c 40 30 60
    d 10 20  0
    e 10 20  0
    f 50  0  0
  </code></pre>