I have a data.table
with two fields, startvalue
and endValue
, that I need to populate based in some information from the previous and actual rows. While this is somehow similar to this, and this, I haven't been able to get the results I want.
Dummy Data:
a <- data.table(user = c("A", "A", "A", "B", "B"),
gap = c(1, 0, 2, 2, 3),
priority = c(1, 3, 2, 2, 1))
Then I fix the startValue
for all priorities == 1:
setkey(a, user, priority)
a[priority == 1, startValue := 0]
and I set the endValue
for those which startValue
is already defined:
a[!is.na(startValue), endValue := startValue + gap*3]
Now comes the problem. I want the startValue
in row 2 (user A, priority 2) to be the same as endValue
of row 1, so I can calculate the new endValue
. I know I can use a loop, but I wanted to know if it's possible to do it by using any other function or combination of functions.
I tried several combinations of shift
and zoo:na.locf
but always ended up messing the already existing values.
Expected result:
b <- structure(list(user = c("A", "A", "A", "B", "B"),
gap = c(1, 2, 0, 3, 2),
priority = c(1, 2, 3, 1, 2),
startValue = c(0, 3, 9, 0, 9),
endValue = c(3, 9, 9, 9, 15)),
row.names = c(NA, -5L),
class = c("data.table", "data.frame"))
We could use accumulate
from purrr
library(purrr)
library(data.table)
a[, endValue := accumulate(gap, ~ .x + .y * 3, .init = 0)[-1], user
][, startValue := shift(endValue, fill = 0), user][]
all.equal(a, b, check.attributes = FALSE)
#[1] TRUE
Or use Reduce
from base R
to create the 'endValue' column and then take the lag
of the 'endValue' for creating the 'startValue' grouped by 'user'
a[, endValue := Reduce(function(x, y) x + y *3, gap,
accumulate = TRUE, init = 0)[-1], user]