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
Using match:
match
dat[, numnextweek := numviews[match(week + 1, week)] , by=Userid] dat[, difference := numviews - numnextweek , by=Userid] dat[xeventinweek != 0] # Userid week xeventinweek numviews numnextweek difference #1: Alice 1 2 5 3 2 #2: Alice 4 1 6 NA NA #3: Bob 2 2 3 5 -2