I have a data table:
> (mydt <- data.table(id=c(1,1,1,1,2,2), time=1:6, v1=letters[1:6], v2=LETTERS[1:6], key=c("id","time"))) id time v1 v2 1: 1 1 a A 2: 1 2 b B 3: 1 3 c C 4: 1 4 d D 5: 2 5 e E 6: 2 6 f F
I want to “collapse this” (is this the correct term here?), To the “change” table: object 1 changed 3 times (from timestamps from 1 to 2, from 2 to 3 and from 3 to 4), object 2 changed once ( time 5-6); I'm interested in initial v1 and final v2 . So, the result should be:
> (res <- data.table(beg.time=c(1,2,3,5), end.time=c(2,3,4,6), v1=c('a','b','c','e'), v2=c('B','C','D','F'), key=c("beg.time","end.time"))) beg.time end.time v1 v2 1: 1 2 a B 2: 2 3 b C 3: 3 4 c D 4: 5 6 e F
source share