Assume we have data like this:
tmp <- data.table(id1 = c(1,1,1,1,2,2,2,3,3), time=c(1,2,3,4,1,2,3,1,2), user_id=c(1,1,1,1,2,2,2,1,1) )
for eac