Noah Noah - 1 month ago 22
R Question

"non-standard" functions within dplyr summarise function

Say I have the following data frame:

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


What I'd like to do is group by Name and have the different instruments
concatenated into a single string like:

Name Instruments
1 John Guitar,Piano,Drums
2 Paul Piano,Bass
3 George Guitar,Sitar
4 Ringo Drums


I figured using group_by and paste within summarise ought to do the trick:

library(dplyr)
df <- df %>%
group_by(Name) %>%
summarise(Instruments = paste(Instrument,sep=","))


but instead, I get an "expecting a single value" error. Is group_by only meant to work with numeric functions, and if so, does anyone know if there's a work around?

Answer
df <- df %>%
  group_by(Name) %>%
  summarise(Instruments = paste(Instrument ,collapse=" "))

note the collapse