我有下一张桌子
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"))
答案 0 :(得分:3)
你可以尝试
library(data.table)
setDT(tb)[, list(Resume=toString(Char)), Name]
或使用aggregate
base R
aggregate(Char~Name, tb, paste, collapse=',')