Cumulative paste (concatenation) of values ​​grouped by another variable

I have a problem with a data frame in R. I would like to insert the contents of the cells into different rows together based on the values ​​of the cells in another column. My problem is that I want the result to be gradually (cumulatively) printed. The output vector must be the same length as the input vector. Here is a sampel table similar to the one I'm dealing with:

id <- c("a", "a", "a", "b", "b", "b") content <- c("A", "B", "A", "B", "C", "B") (testdf <- data.frame(id, content, stringsAsFactors=FALSE)) # id content #1 a A #2 a B #3 a A #4 b B #5 b C #6 b B 

And I want the result to look like this:

 result <- c("A", "AB", "ABA", "B", "BC", "BCB") result #[1] "A" "AB" "ABA" "B" "BC" "BCB" 

That I DO NOT need something like this:

 ddply(testdf, .(id), summarize, content_concatenated = paste(content, collapse = " ")) # id content_concatenated #1 a ABA #2 b BCB 
+6
r dataframe
source share
4 answers

You can define the function of "cumulative paste" using Reduce :

 cumpaste = function(x, .sep = " ") Reduce(function(x1, x2) paste(x1, x2, sep = .sep), x, accumulate = TRUE) cumpaste(letters[1:3], "; ") #[1] "a" "a; b" "a; b; c" 
Cycle

Reduce avoids the repeated concatenation of elements from the very beginning, as it lengthens the previous concatenation with the next element.

Its application by group:

 ave(as.character(testdf$content), testdf$id, FUN = cumpaste) #[1] "A" "AB" "ABA" "B" "BC" "BCB" 

Another idea would be to concatenate the entire vector at the beginning and then gradually substring :

 cumpaste2 = function(x, .sep = " ") { concat = paste(x, collapse = .sep) substring(concat, 1L, cumsum(c(nchar(x[[1L]]), nchar(x[-1L]) + nchar(.sep)))) } cumpaste2(letters[1:3], " ;@-") #[1] "a" "a ;@-b" "a ;@-b ;@-c" 

This also looks a bit faster:

 set.seed(077) X = replicate(1e3, paste(sample(letters, sample(0:5, 1), TRUE), collapse = "")) identical(cumpaste(X, " --- "), cumpaste2(X, " --- ")) #[1] TRUE microbenchmark::microbenchmark(cumpaste(X, " --- "), cumpaste2(X, " --- "), times = 30) #Unit: milliseconds # expr min lq mean median uq max neval cld # cumpaste(X, " --- ") 21.19967 21.82295 26.47899 24.83196 30.34068 39.86275 30 b # cumpaste2(X, " --- ") 14.41291 14.92378 16.87865 16.03339 18.56703 23.22958 30 a 

... which makes it cumpaste_faster .

+22
source share

Here's the ddply method, using sapply and a subset for gradual nesting:

 library(plyr) ddply(testdf, .(id), mutate, content_concatenated = sapply(seq_along(content), function(x) paste(content[seq(x)], collapse = " "))) id content content_concatenated 1 a AA 2 a BAB 3 a AABA 4 b BB 5 b CBC 6 b BBCB 
+2
source share

You can also try dplyr

  library(dplyr) res <- testdf%>% mutate(n=row_number()) %>% group_by(id) %>% mutate(n1=n[1L]) %>% rowwise() %>% do(data.frame(cont_concat= paste(content[.$n1:.$n],collapse=" "),stringsAsFactors=F)) res$cont_concat #[1] "A" "AB" "ABA" "B" "BC" "BCB" 
+2
source share

data.table solution

 library(data.table) setDT(testdf)[, content2 := sapply(seq_len(.N), function(x) paste(content[seq_len(x)], collapse = " ")), by = id] testdf ## id content content2 ## 1: a AA ## 2: a BAB ## 3: a AABA ## 4: b BB ## 5: b CBC ## 6: b BBCB 
+1
source share

All Articles