I have a data frame containing number of page views per user, per week. I want to determine, for each user, whether their views increased, decreased, or stayed the same after a
Since version v1.9.6 (on CRAN 19 Sep 2015), the shift() function is available in data.table:
shift()
data.table
DT[, difference := shift(numviews, type = "lead") - numviews, by = Userid][ xeventinweek != 0L]
Userid week xeventinweek numviews difference 1: Alice 1 2 5 -2 2: Alice 4 1 6 NA 3: Bob 2 2 3 2