I'm trying to update the values of a given column based on values stored in the previous row but from different columns.
I can do it using a for loop that works well with small data sets but when dealing with large DT (say over 1MM rows) this procedure off course takes ages. The following is a small example:
library(data.table)
DT <- data.table(Year = 2019:2038, Area = 500, Cos = c(0,0,0,150,0,0,
0,0,350,0,0,0,0,0,0,0,120,200,80,100), Rep = c(0,0,0,0,150,0,0,0,0,
350,0,0,0,0,0,0,0,0,0,0), Calc = c(500,500,500,500,500,500,500,500,
500,500,500,500,500,500,500,500,500,380,180,100))
Basically, I want to replicate the column "Calc" which is calculated as follow:
1) If row == 1
Calc[1] == Area[1]
2) For rows > 1
Calc[i] == Rep[i] + Calc[i-1] - Cos[i-1]
I would appreciate any feedback
Many thanks