Below is my dataframe, I\'d like to get the \"yes\" column. I can\'t seem to get the cumsum
to reset when it hits the 0 based on the \"value\" field by \"id\". Th
You can create a new by variable on the fly like this:
test[, wrong := cumsum(value), by=.(id, tempID=cumsum(value==0))]
test
id value correct wrong
1: 1 1 1 1
2: 1 1 2 2
3: 1 0 0 0
4: 1 1 1 1
5: 2 1 1 1
6: 2 1 2 2
7: 2 1 3 3
8: 2 1 4 4
9: 3 0 0 0
10: 3 1 1 1
11: 3 1 2 2
12: 3 0 0 0
13: 4 1 1 1
14: 4 1 2 2
15: 4 0 0 0
16: 4 0 0 0
Note that test <-
is not necessary here, as :=
will update the data.table by reference.