rollmean {zoo} | R Documentation |
Generic functions for computing rolling means, maximums and medians of ordered observations.
rollmean(x, k, fill = if (na.pad) NA, na.pad = FALSE, align = c("center", "left", "right"), ...) rollmeanr(..., align = "right") rollmax(x, k, fill = if (na.pad) NA, na.pad = FALSE, align = c("center", "left", "right"), ...) rollmaxr(..., align = "right") rollmedian(x, k, fill = if (na.pad) NA, na.pad = FALSE, align = c("center", "left", "right"), ...) rollmedianr(..., align = "right")
x |
an object (representing a series of observations). |
k |
integer width of the rolling window. Must be odd for |
fill |
a three-component vector or list (recycled otherwise) providing
filling values at the left/within/to the right of the data range.
See the |
na.pad |
deprecated. Use |
align |
character specifying whether the index of the result should be left- or right-aligned or centered (default) compared to the rolling window of observations. |
... |
Further arguments passed to methods. |
These functions compute rolling means, maximums and medians respectively
and are thus similar to rollapply
but are
optimized for speed.
Currently, there are methods for "zoo"
and "ts"
series and
default methods. The default method of rollmedian
is an interface to runmed
.
The default method of rollmean
does not handle inputs that contain
NA
s. In such cases, use rollapply
instead.
An object of the same class as x
with the rolling mean/max/median.
x.Date <- as.Date(paste(2004, rep(1:4, 4:1), sample(1:28, 10), sep = "-")) x <- zoo(rnorm(12), x.Date) rollmean(x, 3) rollmax(x, 3) rollmedian(x, 3) xm <- zoo(matrix(1:12, 4, 3), x.Date[1:4]) rollmean(xm, 3) rollmax(xm, 3) rollmedian(xm, 3) rollapply(xm, 3, mean) # uses rollmean rollapply(xm, 3, function(x) mean(x)) # does not use rollmean