Reputation: 345
I have a dataframe
> df
A B
1 a x
2 b y
3 c z
4 d n
5 e m
I would like the add up the previous value in column A with the current value in column B to replace the current column A, so that the desired output becomes
> df
A B
1 a x
2 a+y y
3 a+y+z z
4 a+y+z+n n
5 a+y+z+n+m m
Code to create the dataframe
df = data.frame(A = c('a','b','c', 'd', 'e'), B = c('x', 'y', 'z', 'n', 'm'))
I wrote for loop
for(i in df){
df$A = lag(df$A) + df$B
}
but it did not work
Edit: The actual values are numeric. I use letters for you to read it quickly. (And perhaps I shouldn't!)
Upvotes: 0
Views: 1384
Reputation: 388982
We can use Reduce
with accumulate = TRUE
Reduce(function(x, y) paste(x, y, sep = "+"), df$B[-1], accumulate = TRUE,
init = df$A[1])
#[1] "a" "a+y" "a+y+z" "a+y+z+n" "a+y+z+n+m"
Similarly, we can also use accumulate
from purrr
library(dplyr)
library(purrr)
df %>% mutate(A = accumulate(B[-1], paste, sep = "+", .init = first(A)))
# A B
#1 a x
#2 a+y y
#3 a+y+z z
#4 a+y+z+n n
#5 a+y+z+n+m m
data
df <- data.frame(A = c('a','b','c', 'd', 'e'), B = c('x', 'y', 'z', 'n', 'm'),
stringsAsFactors = FALSE)
Upvotes: 2
Reputation: 2253
Heres an answer using a for
loop:
# need to make sure they are not factors
df = data.frame(A = c('a','b','c', 'd', 'e'),
B = c('x', 'y', 'z', 'n', 'm'),
stringsAsFactors = F)
# start at 2, not 1, then get the previous row within the loop itself
for (i in 2:nrow(df)){
df$A[i] <- paste0(df$A[i-1], '+', df$B[i])
}
If you want this to work with numeric data, then use
for (i in 2:nrow(df)){
df$A[i] <- df$A[i-1] + df$B[i]
}
Upvotes: 1
Reputation: 50678
You can use cumsum
. Here is a minimal example using some numeric
data
df <- data.frame(A = 1:5, B = 6:10)
In base R
transform(df, A = A[1] + cumsum(c(0, B[-1])))
# A B
#1 1 6
#2 8 7
#3 16 8
#4 25 9
#5 35 10
Or using dplyr
library(dplyr)
df %>% mutate(A = A[1] + cumsum(c(0, B[-1])))
giving the same result.
Upvotes: 1