I am looking for a fast way to implement a rolling sum on a big database. What I want is a fast function that supports both left (and right) alignment and an arguement for min observation.
Essentially, I want to calculate the rolling sum even if the sample size is smaller than the specified width. In the example below, I want the sum of next 5 values whenever next 5 values are available, and if the size of the leading values is smaller than 5, then summing the roll over all that is left.
Example:
x <- seq(1:10)
Desired output:
15 20 25 30 35 40 34 27 19 10
I know that rollapply(x,5, sum, align = "left", partial=1) procudes the desired output, but am looking for a faster solution.
frollsum from the package data.table is fast but does not seem to have an arguement for min observation. roll_sum from the package roll is also fast and accepts a value for min observation, but does not support left alignment.
Thanks in advance.