r - Collapse text by group in data frame -


how aggregate data frame group in column group , collapse text in column text?

sample data:

df <- read.table(header=t, text=" group text a1 a2 a3 b b1 b b2 c c1 c c2 c c3 ") 

required output (data frame):

group text     a1a2a3 b     b1b2 c     c1c2c3 

now have:

sapply(unique(df$group), function(x) {   paste0(df[df$group==x,"text"], collapse='') }) 

this works extent returns text collapsed group, vector:

[1] "a1a2a3" "b1b2"   "c1c2c3" 

i need data frame group column result.

simply use aggregate :

aggregate(df$text, list(df$group), paste, collapse="") ##   group.1      x ## 1       a1a2a3 ## 2       b   b1b2 ## 3       c c1c2c3 

or plyr

library(plyr) ddply(df, .(group), summarize, text=paste(text, collapse="")) ##   group   text ## 1     a1a2a3 ## 2     b   b1b2 ## 3     c c1c2c3 

ddply faster aggregate if have large dataset.

edit : suggestion @sedur :

aggregate(text ~ group, data = df, fun = paste, collapse = "") ##   group   text ## 1     a1a2a3 ## 2     b   b1b2 ## 3     c c1c2c3 

for same result earlier method have :

aggregate(x=list(text=df$text), by=list(group=df$group), paste, collapse="") 

edit2 : data.table :

library("data.table") dt <- as.data.table(df) dt[, list(text = paste(text, collapse="")), = group] ##    group   text ## 1:     a1a2a3 ## 2:     b   b1b2 ## 3:     c c1c2c3 

Comments