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?
n
One way is to use rollapply from zoo:
rollapply
zoo
rollapply(v, width=n, FUN=sum, by=n) # [1] 55 155 255 355 455 555 655 755 855 955
And in case length(v) is not a multiple of n:
length(v)
v <- 1:92 rollapply(v, width=n, FUN=sum, by=n, partial=T, align="left") # [1] 55 155 255 355 455 555 655 755 855 183