1
votes

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?

2

2 Answers

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

note the collapse

2
votes

It may be also useful to nest the 'Instrument' as a list

library(dplyr)
library(tidyr)
df %>% 
   group_by(Name) %>%
   nest(Instrument)

If we need to paste them together, toString is a convenient wrapper for paste(..., collapse=", ").