在一列中加入常用信息

时间:2015-04-13 21:08:34

标签: r

我有下一张桌子

tb <- data.frame(Name=c(rep("John",4),rep("Peter",2),rep("Mary",3)),
             Char=c("CharA","CharB","Charc","CharD","CharB","CharE","CharA","CharC","CharD")
             )

是否可以创建这样的表来汇总每个名字第二列中的信息?

 tb.res <- data.frame(Name=c("John","Peter","Mary"),
                 Resume=c("CharA,CharB,CharC,CharD","CharB,CharE","CharA,CharC,CharD"))

1 个答案:

答案 0 :(得分:3)

你可以尝试

library(data.table)
setDT(tb)[, list(Resume=toString(Char)), Name]

或使用aggregate

中的base R
aggregate(Char~Name, tb, paste, collapse=',')