Noah Noah - 2 months ago 37
R Question

"non-standard" functions within dplyr summarise function

Say I have the following data frame:

> 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:

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?

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

note the collapse