I have a problem dealing with a data frame in R. I would like to paste the contents of cells in different rows together based on the values of the cells in another column. My problem is that I want the output to be progressively (cumulatively) printed. The output vector must be of the same length as the input vector. Here is a sampel table similar to the one I am 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 this is want I want the result to look like:
result <- c("A", "A B", "A B A", "B", "B C", "B C B")
result
#[1] "A" "A B" "A B A" "B" "B C" "B C B"
What I do NOT need something like this:
ddply(testdf, .(id), summarize, content_concatenated = paste(content, collapse = " "))
# id content_concatenated
#1 a A B A
#2 b B C B
Reduce
:ave(as.character(testdf$content), testdf$id, FUN = function(x) Reduce(paste, x, acc = T))
– Tysontyumen