-1

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.

leoce
  • 715
  • 1
  • 8
  • 24

1 Answers1

2

We can paste the elements in 'y' together grouped by 'x'. For this, we can use one of the group by methods. Using data.table, we convert the 'data.frame' to 'data.table' (setDT(df)), grouped by 'x', we paste the 'y' elements within that group.

 library(data.table)
 setDT(df)[, list(y= toString(y)), by = x]

Or using dplyr, we use the same methodology.

 library(dplyr)
 df %>%
    group_by(x) %>%
    summarise(y= toString(y))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for the help! although the returned y is string, I need to `as.numeric(unlist(strsplit(y, split = ', ')))` so y is ready for future calculation. – leoce Dec 16 '15 at 00:52
  • I also updated my question, because I still don't understand why what I did couldn't group the data by x. Could you shed some light on it? Thanks! – leoce Dec 16 '15 at 00:58
  • @leoce The solutions in my post gives `y` as a `character` string. If you wanted `y` to be `numeric`, you can place it in a `list` (as RichardScriven mentioned), i.e. `setDT(df)[, list(y=list(y)), by = x]` – akrun Dec 16 '15 at 02:22
  • Thanks a lot! data.table is also great for the further data working down this line. – leoce Dec 17 '15 at 06:16