Sum every nth points

前端 未结 9 1059
耶瑟儿~
耶瑟儿~ 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:25

    v <- 1:100
    
    n <- 10
    
    cutpoints <- seq( 1 , length( v ) , by = n )
    
    categories <- findInterval( 1:length( v ) , cutpoints )
    
    tapply( v , categories , sum )
    

提交回复
热议问题