For a longitudinal dataset, I want to carry forward observations that terminated before day 7 with y=3, completing records with consecutive days up until day 7 with y=3. A related question is at How to make continuous time sequences within groups in data.table? . The following solution works but I would like to also have a solution that (1) subsetted the observations earlier (see below) or that (2) did the carry forward with a join in one step.
d <- data.table(t =c(1, 2, 1, 2, 3, 1, 2, 1, 2, 3, 5, 6, 7, 1, 2, 3, 5, 6),
id=c(1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5),
y =c(1, 2, 1, 2, 3, 1, 1, 1, 2, 2, 3, 3, 3, 1, 2, 2, 2, 3),
x =c(0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0),
key=c('id', 't'))
d
t id y x
1: 1 1 1 0
2: 2 1 2 0
3: 1 2 1 1
4: 2 2 2 1
5: 3 2 3 1
6: 1 3 1 0
7: 2 3 1 0
8: 1 4 1 1
9: 2 4 2 1
10: 3 4 2 1
11: 5 4 3 1
12: 6 4 3 1
13: 7 4 3 1
14: 1 5 1 0
15: 2 5 2 0
16: 3 5 2 0
17: 5 5 2 0
18: 6 5 3 0
w <- d[, .(tlast=t, last3 = t == max(t) & y == 3 & t < 7, x=x), by=id]
w <- w[last3 == TRUE, .(t = (tlast + 1) : 7, y=rep(3, 7 - tlast), x=x), by=id]
d <- rbind(d, w)
setkey(d, id, t)
d
t id y x
1: 1 1 1 0
2: 2 1 2 0
3: 1 2 1 1
4: 2 2 2 1
5: 3 2 3 1
6: 4 2 3 1
7: 5 2 3 1
8: 6 2 3 1
9: 7 2 3 1
10: 1 3 1 0
11: 2 3 1 0
12: 1 4 1 1
13: 2 4 2 1
14: 3 4 2 1
15: 5 4 3 1
16: 6 4 3 1
17: 7 4 3 1
18: 1 5 1 0
19: 2 5 2 0
20: 3 5 2 0
21: 5 5 2 0
22: 6 5 3 0
23: 7 5 3 0
t id y x
The following doesn't work (results in data.table with 0 rows and 4 cols)
w <- d[(t == max(t) & y == 3 & t < 7) == TRUE, .SD, by=id]