Cumulative sum that resets when 0 is encountered

后端 未结 4 1912
有刺的猬
有刺的猬 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:31

    Another base would be just

    with(df, ave(b, cumsum(b == 0), FUN = cumsum))
    ## [1] 1 0 1 2
    

    This will just divide column b to groups according to 0 appearances and compute the cumulative sum of b per these groups


    Another solution using the latest data.table version (v 1.9.6+)

    library(data.table) ## v 1.9.6+
    setDT(df)[, whatiwant := cumsum(b), by = rleid(b == 0L)]
    #    campaign  date b whatiwant
    # 1:        a   jan 1         1
    # 2:        b   feb 0         0
    # 3:        c march 1         1
    # 4:        d april 1         2
    

    Some benchmarks per comments

    set.seed(123)
    x <- sample(0:1e3, 1e7, replace = TRUE)
    system.time(res1 <- ave(x, cumsum(x == 0), FUN = cumsum))
    # user  system elapsed 
    # 1.54    0.24    1.81 
    system.time(res2 <- Reduce(function(x, y) if (y == 0) 0 else x+y, x, accumulate=TRUE))
    # user  system elapsed 
    # 33.94    0.39   34.85 
    library(data.table)
    system.time(res3 <- data.table(x)[, whatiwant := cumsum(x), by = rleid(x == 0L)])
    # user  system elapsed 
    # 0.20    0.00    0.21 
    
    identical(res1, as.integer(res2))
    ## [1] TRUE
    identical(res1, res3$whatiwant)
    ## [1] TRUE
    

提交回复
热议问题