Hi, I have created a lagged variable using instructions from the answer to the above question (link above). It says that to create a lagged variable I need to use:
library(data.table)
data = data[, lag.value:=c(NA, value[-.N]), by=groups]
Or, alternatively:
data = data[, lag.value := shift(value, 1L), keyby = groups]
This is what I got from the related questions answers below:
(I might be not entirely right with the second method because it is a bit complicated there so please correct me if it's wrong)
In any case as I use any of these methods I get an error:
Error in `[.data.frame`(data, , `:=`(lag.value, c(NA, :
unused argument (by = groups)
Could you please explain what I'm doing wrong here and what I should do to avoid the error?
data:
time value groups
1 3 a
2 3 a
3 4 a
4 4 a
1 1 b
2 2 b
3 5 b
4 5 b
and the variable I want to create is lag.value which is value lagged by 1 within groups