title: How to combine multiple rows into one observation in R
body: <p>I am relatively new to R and I am kind of hung up at trying to put my data into a suitable format. It seems like the reshape package might be useful for this, but I don't get any further than that.</p>
  
  <p>I have a data frame in which one of the columns (V4) contains strings and numericals. I would like to split V4 by the grouping given in V2 and V1 and attach the results as three seperate columns to the data frame.</p>
  
  <p>Edit: As my original example data frame did not quite capture the complexity of the problem, here is a more accurate example:</p>
  
  <pre><code>&gt;df &lt;- data.frame(V1=c(rep("SN", 8),rep("JK", 4)), 
               V2=c(1,1,2,2,2,3,3,3,1,1,2,2), 
               V3=c("Picture", "Response", "Sound", "Sound", "Response", "Sound", "Sound", "Response", "Sound", "Response", "Sound", "Sound"), 
               V4=c("Photo", "100", "XYZc02i03", "XYZq02i03", 200, "ZYXc01i30", "ZYXq01i30", 100, "XYZc02i40", 200, "XYZc02i03", "XYZq02i03" ), 
               stringsAsFactors=FALSE)
  
  
  &gt;V1 V2       V3        V4
   SN  1  Picture     Photo
   SN  1 Response       100
   SN  2    Sound XYZc02i03
   SN  2    Sound XYZq02i03
   SN  2 Response       200
   SN  3    Sound ZYXc01i30
   SN  3    Sound ZYXq01i30
   SN  3 Response       100
   JK  1    Sound XYZc02i40
   JK  1 Response       200
   JK  2    Sound XYZc02i03
   JK  2    Sound XYZq02i03
  </code></pre>
  
  <p>And I want to get something like this:</p>
  
  <pre><code>   V1  V2       V3          V4        V5   V6
     SN   1  Picture       Photo        NA  100
     SN   2    Sound   XYZc02i03 XYZq02i03  200
     SN   3    Sound   ZYXc01i30 ZYXq01i30  100
     JK   1    Sound   XYZc02i40        NA  200
     JK   2    Sound   XYZc02i03 XYZq02i03   NA
  </code></pre>
  
  <p>EDIT: I don't always have the same number of observations in V2, which means there could be missing values for V4, V5, or V6 in the data frame I want to get. </p>
  
  <p>Edit2: V6 should map onto the "response" Variable from V3, V4 and V5 ideally map on the "Sound" values from V3 in consecutive order.</p>
  
  <p>I would be very appreciative of any advice on how to go about this. Or, if this problem has been solved elswhere and I missed it, a link would also be great.</p>