我有以下data.table與感興趣的變量x
。我想創建另一個變量,指示從0到1的x
跳轉,這意味着該變量在某年之後一直爲0,在此之後的所有年份中爲1。這應該由id_d
完成。使用data.table識別從0跳躍到1
是否有一個簡單的data.table方法來做到這一點?
原始data.table:
fullDat <- data.table(id_d = rep(letters[1:3], each=12),
year=rep(1:12, 3),
x = c(rep(0, 5), rep(1, 7), 0,1,0,1,2,2,4, rep(5,5), 1, rep(0, 3), rep(1, 8)))
id_d year x
1: a 1 0
2: a 2 0
3: a 3 0
4: a 4 0
5: a 5 0
6: a 6 1
7: a 7 1
8: a 8 1
9: a 9 1
10: a 10 1
11: a 11 1
12: a 12 1
13: b 1 0
14: b 2 1
15: b 3 0
16: b 4 1
17: b 5 2
18: b 6 2
19: b 7 4
20: b 8 5
21: b 9 5
22: b 10 5
23: b 11 5
24: b 12 5
25: c 1 1
26: c 2 0
27: c 3 0
28: c 4 0
29: c 5 1
30: c 6 1
31: c 7 1
32: c 8 1
33: c 9 1
34: c 10 1
35: c 11 1
36: c 12 1
id_d year x
的結果應該是什麼樣子:
id_d year x jump
1: a 1 0 0
2: a 2 0 0
3: a 3 0 0
4: a 4 0 0
5: a 5 0 0
6: a 6 1 1
7: a 7 1 0
8: a 8 1 0
9: a 9 1 0
10: a 10 1 0
11: a 11 1 0
12: a 12 1 0
13: b 1 0 0
14: b 2 1 0
15: b 3 0 0
16: b 4 1 0
17: b 5 2 0
18: b 6 2 0
19: b 7 4 0
20: b 8 5 0
21: b 9 5 0
22: b 10 5 0
23: b 11 5 0
24: b 12 5 0
25: c 1 1 0
26: c 2 0 0
27: c 3 0 0
28: c 4 0 0
29: c 5 1 0
30: c 6 1 0
31: c 7 1 0
32: c 8 1 0
33: c 9 1 0
34: c 10 1 0
35: c 11 1 0
36: c 12 1 0
id_d year x jump
Fyi,這會在DT = data.table(id_d = rep(「d」,4),year = 1:4,x = c(0,1, 0,1))'(實際上對於長「與緊湊」版本給出了不同的結果) – Frank
@Frank我沒有測試所有的情況 – akrun