In a table with a rb_date column and a value column, I want to calculate the price level (level). Assume the starting base level is 100, the price level within each period (meaning rows with same rb_date) is simply return * base level, and each period's base level is the last value of previous period's price level. I am able to realize it by using data.table. How could I do it in dplyr?
Below is a reproducible example of how I did it in data.table.
library(data.table)
library(dplyr)
table <- tibble(rb_date = rep(c("2017-01-01", "2017-02-01", "2017-03-01"), 3),
return = rnorm(9)) %>% arrange(rb_date)
rb_dates = sort(unique(table$rb_date))
table = as.data.table(table)
level_lag = 100
for (i in 1:length(rb_dates)) {
table[rb_date == rb_dates[i], level := level_lag * (1 + return)]
level_lag = table[rb_date == rb_date[i], last(level)]
}
?dplyr::laguseful? - spinodal