3

I've been using the RcppRoll package to do rolling sums, but I'd like to find a function/option that would allow me to turn a vector like this [1 2 3 4 5] into [1 3 6 9 12] for a window of 3, for instance.

Currently, I've been using roll_sum, which returns [NA NA 6 9 12] for a window of 3.

It seems like it would work to use something like rollapplyr(x, 3, sum, partial=TRUE), but that's just far too slow for something I'll eventually need to run on millions of rows.

dc4
  • 31
  • 1

1 Answers1

4

Insert zeros at the beginning of the input:

width <- 3
roll_sum(c(numeric(width-1), 1:5), width)
## [1]  1  3  6  9 12
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341