Apply Rolling Functions
A generic function for applying a function to rolling margins of an array.
rollapply(data, width, FUN, ..., by = 1, ascending = TRUE, by.column = TRUE, na.pad = FALSE, align = c("center", "left", "right"))
- the data to be used (representing a series of observations).
- number of points per group.
- the function to be applied.
In the case of functions like
%*%, etc., the function name must be quoted.
- optional arguments to
- calculate FUN for trailing width points at every by-th time point.
- logical. If TRUE then points are passed to
FUNin ascending order of time; otherwise, they are passed in descending order.
- logical. If
FUNis applied to each column separately.
- logical. If
TRUEthen additional elements or rows of
NAs are added so that result has same number of elements or rows as
- character specifying whether result should be left- or right-aligned or centered (default).
Groups time points in successive sets of
width time points and
FUN to the corresponding values. If
TRUE and there are no extra arguments
then special purpose code is used to enhance performance.
for more details.
Currently, there are methods for
In previous versions, this function was called
rapply. It was renamed
because from R 2.4.0 on, base R provides a different function
for recursive (and not rolling) application of functions. The function
zoo::rapply is still provided for backward compatibility, however
it dispatches now to
- A object of the same class as
datawith the results of the rolling function.
z <- zoo(11:15, as.Date(31:35)) rollapply(z, 2, mean) z2 <- zoo(rnorm(6)) rollapply(z2, 3, mean, by = 3) # means of nonoverlapping groups of 3 aggregate(z2, c(3,3,3,6,6,6), mean) # same rollapply(z2, 3, mean) # uses rollmean which is optimized for mean rollmean(z2, 3) # same rollapply(z2, 3, (mean)) # does not use rollmean