# rollmean: Rolling Means/Maximums/Medians/Sums In zoo: S3 Infrastructure for Regular and Irregular Time Series (Z's Ordered Observations)

## Description

Generic functions for computing rolling means, maximums, medians, and sums of ordered observations.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16``` ```rollmean(x, k, fill = if (na.pad) NA, na.pad = FALSE, align = c("center", "left", "right"), ...) rollmax(x, k, fill = if (na.pad) NA, na.pad = FALSE, align = c("center", "left", "right"), ...) rollmedian(x, k, fill = if (na.pad) NA, na.pad = FALSE, align = c("center", "left", "right"), ...) rollsum(x, k, fill = if (na.pad) NA, na.pad = FALSE, align = c("center", "left", "right"), ...) rollmeanr(..., align = "right") rollmaxr(..., align = "right") rollmedianr(..., align = "right") rollsumr(..., align = "right") ```

## Arguments

 `x` an object (representing a series of observations). `k` integer width of the rolling window. Must be odd for `rollmedian`. `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 `fill` argument of `na.fill` for details. `na.pad` deprecated. Use `fill = NA` instead of `na.pad = TRUE`. `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.

## Details

These functions compute rolling means, maximums, medians, and sums 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 methods of `rollmean` and `rollsum` do not handle inputs that contain `NA`s. In such cases, use `rollapply` instead.

If `x` is of length 0, `x` is returned unmodified.

## Value

An object of the same class as `x` with the rolling mean/max/median/sum.

`rollapply`, `zoo`, `na.fill`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22``` ```suppressWarnings(RNGversion("3.5.0")) set.seed(1) x.Date <- as.Date(paste(2004, rep(1:4, 4:1), sample(1:28, 10), sep = "-")) x <- zoo(rnorm(12), x.Date) ## rolling operations for univariate series rollmean(x, 3) rollmax(x, 3) rollmedian(x, 3) rollsum(x, 3) ## rolling operations for multivariate series xm <- zoo(matrix(1:12, 4, 3), x.Date[1:4]) rollmean(xm, 3) rollmax(xm, 3) rollmedian(xm, 3) rollsum(xm, 3) ## rollapply vs. dedicated rollmean rollapply(xm, 3, mean) # uses rollmean rollapply(xm, 3, function(x) mean(x)) # does not use rollmean ```

### Example output

```Attaching package: 'zoo'

The following objects are masked from 'package:base':

as.Date, as.Date.numeric

2004-01-11 2004-01-15 2004-01-23 2004-02-05 2004-02-21 2004-02-25 2004-03-13
0.1350951  0.6005117  0.3362392  0.5940580  0.5320787 -0.1043585 -0.8153657
2004-03-14
-0.5703365
2004-01-11 2004-01-15 2004-01-23 2004-02-05 2004-02-21 2004-02-25 2004-03-13
0.7383247  0.7383247  0.7383247  1.5117812  1.5117812  1.5117812  0.3898432
2004-03-14
1.1249309
2004-01-11 2004-01-15 2004-01-23 2004-02-05 2004-02-21 2004-02-25 2004-03-13
0.4874291  0.5757814  0.5757814  0.5757814  0.3898432  0.3898432 -0.6212406
2004-03-14
-0.6212406
2004-01-11 2004-01-15 2004-01-23 2004-02-05 2004-02-21 2004-02-25 2004-03-13
0.4052854  1.8015351  1.0087177  1.7821741  1.5962360 -0.3130755 -2.4460972
2004-03-14
-1.7110095

2004-01-11 2 6 10
2004-01-15 3 7 11

2004-01-11 3 7 11
2004-01-15 4 8 12

2004-01-11 2 6 10
2004-01-15 3 7 11

2004-01-11 6 18 30
2004-01-15 9 21 33

2004-01-11 2 6 10
2004-01-15 3 7 11

2004-01-11 2 6 10
2004-01-15 3 7 11
```

zoo documentation built on March 9, 2021, 5:07 p.m.