How to create a lag variable in unbalanced time series table (unevenly spaced in time) ? From data table -
ts = data.table(time=c(10,15,22,25,28),value=c(7,2,14,22,11), key="time")
How do you create lagged variable value_lagged
of value
by time-5
?
time value value_lagged
10 7 NA
15 2 7
22 14 2
25 22 2
28 11 14
My usual solution is to create a huge cross-join table and then use 'by' + 'min' to get back to initial state, but this doesn't work for 1M tables.
This question has an answer here - https://stackoverflow.com/a/36754846/1317325 but too hard to find.