[R] dynamic runSum

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 8 18:39:24 CEST 2014

On Thu, Aug 7, 2014 at 9:32 AM, amarjit chandhial
<a.chandhial at btinternet.com> wrote:
> Hello,
> runSum calculates a running sum looking back a fixed distance n, e.g. 20.
> How do I calculate a dynamic runSum function for an xts object?
> In
> otherwords, I want to calculate a running sum at each point in time
> looking back a variable distance. In this example, values governed by
> the vector VL.

The width argument in rollapplyr in the zoo package can be a vector.
It can't be NA though so we have used 1 in those cases here and at the
end used na.omit to get rid of the junk at the beginning:

na.omit(rollapplyr(acf1, ifelse(is.na(acf1$VL), 1, acf1$VL), sum))

More information about the R-help mailing list