Skip to content
Prev 315086 / 398506 Next

split & rbind (cast) dataframe

Hi Johannes,

Your example is tricky because the original df1 does not contain id
columns that identify each cell in df2. If you want to use the
reshape2 package for this I think you have to add a second id
variable:

df1 <- data.frame(col1 = c("A","A","B","B"),col2 = c(1:4), col3 = c(1:4))

library(reshape2)
library(plyr)

df1 <- ddply(df1,
             "col1",
             transform,
             col0 = 1:length(col1))

tmp <- melt(df1, id.vars = c("col0","col1"))
dcast(tmp, col0 ~ col1 + variable)

Best,
Ista

On Fri, Jan 11, 2013 at 7:46 AM, Johannes Radinger
<johannesradinger at gmail.com> wrote: