Concatenate strings by group with dplyr [duplicate]

ghz 1years ago ⋅ 859 views

Question

This question already has answers here :

[Collapse / concatenate / aggregate a column to a single comma separated string within each group](/questions/15933958/collapse-concatenate-aggregate- a-column-to-a-single-comma-separated-string-w) (6 answers)

Closed 6 years ago.

i have a dataframe that looks like this

> data <- data.frame(foo=c(1, 1, 2, 3, 3, 3), bar=c('a', 'b', 'a', 'b', 'c', 'd'))
> data
  foo bar
1   1   a
2   1   b
3   2   a
4   3   b
5   3   c
6   3   d

I would like to create a new column bars_by_foo which is the concatenation of the values of bar by foo. So the new data should look like this:

  foo bar bars_by_foo
1   1   a          ab
2   1   b          ab
3   2   a           a
4   3   b         bcd
5   3   c         bcd
6   3   d         bcd

I was hoping that the following would work:

p <- function(v) {
  Reduce(f=paste, x = v)
}
data %>% 
  group_by(foo) %>% 
  mutate(bars_by_foo=p(bar))

But that code gives me an error

Error: incompatible types, expecting a character vector.

What am I doing wrong?


Answer

You could simply do

data %>% 
     group_by(foo) %>% 
     mutate(bars_by_foo = paste0(bar, collapse = "")) 

Without any helper functions