Cumulative sum that resets when 0 is encountered

后端 未结 4 1910
有刺的猬
有刺的猬 2020-11-27 19:55

I would like to do a cumulative sum on a field but reset the aggregated value whenever a 0 is encountered.

Here is an example of what I want :

data.f         


        
4条回答
  •  执笔经年
    2020-11-27 20:34

    Another late idea:

    ff = function(x)
    {
        cs = cumsum(x)
        cs - cummax((x == 0) * cs)
    }
    ff(c(0, 1, 3, 0, 0, 5, 2))
    #[1] 0 1 4 0 0 5 7
    

    And to compare:

    library(data.table)
    ffdt = function(x) 
        data.table(x)[, whatiwant := cumsum(x), by = rleid(x == 0L)]$whatiwant
    
    x = as.numeric(x) ##because 'cumsum' causes integer overflow
    identical(ff(x), ffdt(x))
    #[1] TRUE
    microbenchmark::microbenchmark(ff(x), ffdt(x), times = 25)
    #Unit: milliseconds
    #    expr      min       lq   median       uq      max neval
    #   ff(x) 315.8010 362.1089 372.1273 386.3892 405.5218    25
    # ffdt(x) 374.6315 407.2754 417.6675 447.8305 534.8153    25
    

提交回复
热议问题