Cumsum with reset when 0 is encountered and by groups

后端 未结 2 1060
生来不讨喜
生来不讨喜 2021-01-21 14:55

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

2条回答
  •  醉梦人生
    2021-01-21 15:30

    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.

提交回复
热议问题