Sum every nth points

前端 未结 9 1104
耶瑟儿~
耶瑟儿~ 2020-11-29 04:42

I have a vector and I need to sum every n numbers and return the results. This is the way I plan on doing it currently. Any better way to do this?



        
9条回答
  •  孤城傲影
    2020-11-29 05:12

    I will add one more way of doing it without any function from apply family

    v <- 1:100
    n <- 10
    
    diff(c(0, cumsum(v)[slice.index(v, 1)%%n == 0]))
    ##  [1]  55 155 255 355 455 555 655 755 855 955
    

提交回复
热议问题