We can do
DT[, cum.sum := Reduce(`+`, shift(val, 0:3)), by=id]
id val cum.sum
1: A 1 NA
2: A 2 NA
3: A 3 NA
4: A 4 10
5: A 5 14
6: A 6 18
7: B 1 NA
8: B 2 NA
9: C 1 NA
10: C 2 NA
11: C 3 NA
12: C 4 10
13: C 5 14
14: C 6 18
15: C 7 22
16: C 8 26
I knew I'd seen this somewhere before - possibly a duplicate?