use dplyr to concatenate a column

dplyr, r

Solution

You need to collapse the values in paste

df %>% group_by(id) %>% summarise(vector=paste(A, collapse=" "))

Problem

I have a `data_frame` where I would like `vector` to be the concatenation of elements in `A`. So ``` df <- data_frame(id = c(1, 1, 2, 2), A = c("a", "b", "b", "c")) df Source: local data frame [4 x 2] id A 1 1 a 2 1 b 3 2 b 4 2 c ``` Should become ``` newdf Source: local data frame [4 x 2] id vector 1 1 "a b" 2 2 "b c" ``` My first inclination is to use `paste()` inside `summarise` but this doesn't work. ``` df %>% group_by(id) %>% summarise(paste(A)) Error: expecting a single value ``` Hadley and Romain talk about a similar issue in the GitHub issues, but I can't quite see how that applies directly. It seems like there should be a very simple solution, especially because `paste()` usually does return a single value.

Original source

Related problems