I don't know if this is faster, but at least the code is shorter:
sapply(seq_along(df$A),function(x){paste(A[1:x], collapse=" ")})
Thanks to Rolands's remark, I realized that this was one of the rare cases when a for loop could be useful, as it saves us from re-indexing. It differs from OP when it starts with 2, while retaining the need for if status inside forloop.
res <- c(NA, length(df1$A)) res[1] <- as.character(df1$A[1]) for(i in 2:length(df1$A)){ res[i] <- paste(res[i-1],df1$A[i]) } res
Heroka
source share