I have a table with 2 columns and I'd like to combine values of the same factor level in one row, for example, I make a simplifed version here,
> df <- data.frame(x = rep(c('A', 'B', 'C'), 2), y = 1:6)
x y
A 1
B 2
C 3
A 4
B 5
C 6
and I want it to be:
x y
A 1, 4
B 2, 5
C 3, 6
so I tried plyr
:
> library(plyr)
> d <- ddply(df, .(x), summarise, y = c(rbind(y)))
x y
A 1
A 4
B 2
B 5
C 3
C 6
What kind of mistake did I make and how can I get the format I want? I tested with c(rbind(df$y))
before using plyr
and the result seemed plausible while it didn't work out well. Thanks for any help!
Update
I also tried d <- ddply(df, .(x), summarise, y = as.numeric(unlist(strsplit(paste(y), split = ' '))))
, it returned the same result with no aggregation based on x.