I am looking for a way to do what would be the equivalent of a cumulative sum in R for string/character-formatted text instead of numbers. The different text fields should be concatenated.
E.g. in the data frame "df":
Column A contains the input, column B the desired result.
A B
1 banana banana
2 boats banana boats
3 are banana boats are
4 awesome banana boats are awesome
Currently I am solving this via the following loop
df$B <- ""
for(i in 1:nrow(df)) {
if (length(df[i-1,"A"]) > 0) {
df$B[i] <- paste(df$B[i-1],df$A[i])
} else {
df$B[i] <- df$A[i]
}
}
I wonder whether there exists a more elegant/faster solution.