I'm trying to compute a new column in a dataframe that multiply a constant by the value of the previous row (in the new created column B) and accumulate the result.
To clarify, here is an example of my original data:
A
---
100
200
300
400
500
An here is what I'm trying to accomplish:
A B
-----------
100 100.00
200 302.00
300 608.04
400 1020.20
500 1540.60
(The constant being in this case: 0.02)
The (approximate) formula that I am using in excel is something similar to this:
=(B1*(1+constant)+A2) [In Cell B2]
I've tried using lag, mutate and cumsum from dplyr to achieve this, but so far I've had no luck. Here a reproducible example of what I've managed to achieve thus far:
rate = .02
A <- c(100,200,300,400,500)
df <- data.frame(A)
df = df %>%
mutate(B = lag(A,default=0)*(1+rate)) %>%
mutate(B = cumsum(B)+df$A)
This produce the following output (different than what I'm trying to get):
A B
100 100.00
200 302.00
300 606.00
400 1012.00
500 1520.00
Can anyone help me with some code to compute this? Thanks for the help!