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 a concatenation of bar values by foo. Therefore, the new data should look like this:
foo bar bars_by_foo 1 1 a ab 2 1 b ab 3 2 aa 4 3 b bcd 5 3 c bcd 6 3 d bcd
I was hoping the following would work:
p <- function(v) { Reduce(f=paste, x = v) } data %>% group_by(foo) %>% mutate(bars_by_foo=p(bar))
But this code gives me an error
Error: incompatible types, expecting a character vector .
What am I doing wrong?
r dplyr
crf
source share