说我有以下数据框:
df<-data.frame(Name=c(rep("John",3),rep("Paul",2),rep("George",2),"Ringo"),
Instrument=c("Guitar","Piano","Drums","Piano","Bass","Guitar","Sitar","Drums"))
> df
Name Instrument
1 John Guitar
2 John Piano
3 John Drums
4 Paul Piano
5 Paul Bass
6 George Guitar
7 George Sitar
8 Ringo Drums
我想做的是按姓名分组并拥有不同的工具 连接成一个单独的字符串,如:
Name Instruments
1 John Guitar,Piano,Drums
2 Paul Piano,Bass
3 George Guitar,Sitar
4 Ringo Drums
我认为使用group_by并在摘要中粘贴应该可以解决问题:
library(dplyr)
df <- df %>%
group_by(Name) %>%
summarise(Instruments = paste(Instrument,sep=","))
但相反,我得到了“期待单一值”的错误。 group_by是否仅适用于数字函数,如果是,有人知道是否有解决方法吗?
答案 0 :(得分:3)
df <- df %>%
group_by(Name) %>%
summarise(Instruments = paste(Instrument ,collapse=" "))
请注意collapse
答案 1 :(得分:2)
将nest
'工具'作为list
library(dplyr)
library(tidyr)
df %>%
group_by(Name) %>%
nest(Instrument)
如果我们需要paste
他们在一起,toString
是paste(..., collapse=", ")
的便捷包装。