1\name{rollmean}
2
3\alias{rollmean}
4\alias{rollmax}
5\alias{rollmedian}
6\alias{rollsum}
7
8\alias{rollmeanr}
9\alias{rollmaxr}
10\alias{rollmedianr}
11\alias{rollsumr}
12
13\alias{rollmean.zoo}
14\alias{rollmax.zoo}
15\alias{rollmedian.zoo}
16\alias{rollsum.zoo}
17
18\alias{rollmean.ts}
19\alias{rollmax.ts}
20\alias{rollmedian.ts}
21\alias{rollsum.ts}
22
23\alias{rollmean.default}
24\alias{rollmax.default}
25\alias{rollmedian.default}
26\alias{rollsum.default}
27
28\title{Rolling Means/Maximums/Medians/Sums}
29
30\description{
31Generic functions for computing rolling means, maximums, medians, and sums of ordered observations.
32}
33
34\usage{
35rollmean(x, k, fill = if (na.pad) NA, na.pad = FALSE,
36  align = c("center", "left", "right"), ...)
37
38rollmax(x, k, fill = if (na.pad) NA, na.pad = FALSE,
39  align = c("center", "left", "right"), ...)
40
41rollmedian(x, k, fill = if (na.pad) NA, na.pad = FALSE,
42  align = c("center", "left", "right"), ...)
43
44rollsum(x, k, fill = if (na.pad) NA, na.pad = FALSE,
45  align = c("center", "left", "right"), ...)
46
47rollmeanr(..., align = "right")
48rollmaxr(..., align = "right")
49rollmedianr(..., align = "right")
50rollsumr(..., align = "right")
51}
52
53\arguments{
54  \item{x}{an object (representing a series of observations).}
55  \item{k}{integer width of the rolling window. Must be odd for \code{rollmedian}.}
56  \item{fill}{a three-component vector or list (recycled otherwise) providing
57    filling values at the left/within/to the right of the data range.
58    See the \code{fill} argument of \code{\link{na.fill}} for details.}
59  \item{na.pad}{deprecated. Use \code{fill = NA} instead of \code{na.pad = TRUE}.}
60  \item{align}{character specifying whether the index of the result
61    should be left- or right-aligned or centered (default) compared
62    to the rolling window of observations.}
63  \item{\dots}{Further arguments passed to methods.}
64}
65
66\details{
67These functions compute rolling means, maximums, medians, and sums respectively
68and are thus similar to \code{\link{rollapply}} but are
69optimized for speed.
70
71Currently, there are methods for \code{"zoo"} and \code{"ts"} series and
72default methods. The default method of \code{rollmedian}
73is an interface to \code{\link{runmed}}.
74The default methods of \code{rollmean} and \code{rollsum} do not handle inputs that contain
75\code{NA}s. In such cases, use \code{\link{rollapply}} instead.
76
77If \code{x} is of length 0, \code{x} is returned unmodified.
78}
79
80\value{
81An object of the same class as \code{x} with the rolling mean/max/median/sum.
82}
83
84\seealso{\code{\link{rollapply}}, \code{\link{zoo}}, \code{\link{na.fill}}}
85
86\examples{
87suppressWarnings(RNGversion("3.5.0"))
88set.seed(1)
89
90x.Date <- as.Date(paste(2004, rep(1:4, 4:1), sample(1:28, 10), sep = "-"))
91x <- zoo(rnorm(12), x.Date)
92
93## rolling operations for univariate series
94rollmean(x, 3)
95rollmax(x, 3)
96rollmedian(x, 3)
97rollsum(x, 3)
98
99## rolling operations for multivariate series
100xm <- zoo(matrix(1:12, 4, 3), x.Date[1:4])
101rollmean(xm, 3)
102rollmax(xm, 3)
103rollmedian(xm, 3)
104rollsum(xm, 3)
105
106## rollapply vs. dedicated rollmean
107rollapply(xm, 3, mean) # uses rollmean
108rollapply(xm, 3, function(x) mean(x)) # does not use rollmean
109}
110\keyword{ts}
111