Username Username - 10 months ago 38
R Question

R: concatenate string across multiple columns in data frame

I have the following data frame with 3 columns

category <- c("A", "A", "A", "B","B")
id <- c(1,1,2,3,3)
text <- c("abc", "def", "ghi", "jkl", "pqr")
df <- data.frame(category,id,text)

> df
category id text
1 A 1 abc
2 A 1 def
3 A 2 ghi
4 B 3 jkl
5 B 3 pqr


I want to concatenate the text per id per group

My output needs to be like:

A 1 "abc def"
A 2 "ghi"
B 3 "jkl pqr"


I tried using

library(stringr)
str_c(df[,3], collapse = NULL)


But my output is not correct, also how can I get this per id per group

Answer Source

Using dplyr, you can do:

library(dplyr)
df %>% group_by(category,id) %>% summarise(text=paste(text,collapse=" "))

  category    id    text
    <fctr> <dbl>   <chr>
1        A     1 abc def
2        A     2     ghi
3        B     3 jkl pqr
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download