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.