I have a fairly large dataset, and I'm interested in "marching" values forward in time based on values from another column. For example, if I have Value = 3at Time = 0and DesiredShift = 2I want 3 to change two lines to Time = 2. Here is a reproducible example.
Create reproducible fake data
library(data.table)
set.seed(1)
rowsPerID <- 8
dat <- CJ(1:2, 1:rowsPerID)
setnames(dat, c("ID","Time"))
dat[, Value := rpois(.N, 4)]
dat[, Shift := sample(0:2, size=.N, replace=TRUE)]
Fake Data
I want everyone to Valuemove forward according to the column Shift. So the column DesiredOutputfor row 3 will be 3, since the value is Time=1equal to
Value = 3and Shift = 2.
Line 4 shows 3 + 4 = 7, as 3 moves down by 2 and 4 go down.
ID , ,
data.table, .
data.table::shift, , .