-1

I'm doing some data analysis in R, and I have the following data in a data.frame:

  v1 v2
1 11  3
2 11  4
3 11  5
4 15  6
5 15  7
6 20  8

And I would like to transform it into the following:

 col1   col2 col3 col4
 11     3    4    5
 15     6    7    Null
 20     8    Null Null

I'd greatly appreciate it if anyone has any suggestions. I can do it in a for loop, but I'm wondering if there is a built-in function in R.

Thanks.

ProfLonghair
  • 81
  • 1
  • 8
  • See [here](http://stackoverflow.com/questions/11322801/transpose-reshape-dataframe-without-timevar-from-long-to-wide-format) a similar post – alexis_laz Sep 30 '16 at 15:49

1 Answers1

0

You can try a combination of split, do.call, lapply dcast (from reshape2) and rbind.fill (from plyr) to get your desired output:

Data:

df1 <- structure(list(v1 = c(11L, 11L, 11L, 15L, 15L, 20L), v2 = 3:8), .Names = c("v1", 
"v2"), class = "data.frame", row.names = c(NA, -6L))

Code:

do.call(plyr::rbind.fill,lapply(split(df1, df1$v1), function(x) {
    tempdf <- data.frame(reshape2::dcast(x, v1~v2, value.var = "v2"))
    names(tempdf) <- paste("col", 1:ncol(tempdf), sep = "")
    return(tempdf)
}))

Output:

 col1 col2 col3 col4
 11   3     4    5  
 15   6     7   NA  
 20   8    NA   NA 

Explanation:

The idea here to split your data.frame by the column v1; and for each of the subsets you apply a dcast operation with the formula v1 ~ v2. This means to arrange all values in column v2 into one row, prepended by the value in v1. Once you are done casting the subsets, you can then join the results together using rbind.fill from the plyr package.

I hope this helps.

Abdou
  • 12,931
  • 4
  • 39
  • 42