How to combine several lines in one case in R

I'm relatively new to R, and I kind of hung up trying to put my data in a suitable format. It looks like the reshape package might be useful for this, but I don't understand this.

I have a data frame in which one of the columns (V4) contains rows and numeric values. I would like to divide V4 into the grouping specified in V2 and V1, and attach the results as three separate columns to the data frame.

Edit: since my original sample data frame didn't quite complicate the problem, here is a more accurate example:

>df <- 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)


>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

And I want to get something like this:

   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

EDIT: I do not always have the same number of observations in V2, which means that the data frame I want to receive may not have values ​​for V4, V5 or V6.

Edit2: V6 "" V3, V4 V5 "" V3 .

, . , , , .

+2
2

cbind df. - :

df <- data.frame(V1=rep("SN", 6), 
                 V2=rep(2:3, each=3), 
                 V3=c("Sound", "Sound", "Response", "Sound", "Sound", "Response"), 
                 V4=c("XYZc02i03", "XYZq02i03", 200, "ZYXc01i30", "ZYXq01i30", 100), 
                 stringsAsFactors=FALSE)

, , , :

max.subset.len <- 3 # or maybe max(sapply(split(df, list(df$V1, df$V2)), FUN=nrow))
fun <- function(v4) {length(v4) <- max.subset.len; v4}
agg <- aggregate(df$V4, by=list(df$V1, df$V2), FUN=fun)
results <- cbind(agg[1:2], agg[[3]])
0

. - ?

0

All Articles