我有一张这样的桌子:
id words
1 I like school.
2 I hate school.
3 I like cakes.
1 I like cats.
这是我想要做的,根据id加入每行中的字符串。
id words
1 I like school. I like cats.
2 I hate school.
3 I like cakes.
是否有一个包在R?
中执行此操作答案 0 :(得分:1)
我们可以paste
'}'一起按“id'”分组。这可以通过任何分组操作来完成。一种方式是data.table
。我们转换了' data.frame'到' data.table' (setDT(df1)
)然后上面提到了操作。
# install.packages(c("data.table"), dependencies = TRUE)
library(data.table)
setDT(df1)[, list(words = paste(words, collapse=' ')), by = id]
base R
操作将使用aggregate
aggregate(words~id, df1, FUN= paste, collape=' ')