mav: Calculate moving average

View source: R/stats.R

mavR Documentation

Calculate moving average

Description

This function calculates the moving average of a time series or numeric vector. The rolling calculation can be parameterized by the size of the window with the argument window and alignment of the window (centered or left-aligned) with the align parameter.

Usage

mav(x, window, align = "center")

Arguments

x

Numeric vector or time series

window

Numeric vector of length 1 indicating the size of window for the moving average

align

Character vector specifying the alignment of the window; allowed values include "left" or "center"; default is "center"

Details

The functionality here is a wrapper for filter. The align argument corresponds to sides in stats::filter, with align = "left" triggering sides = 1 and align = "center" triggering sides = 2. The function uses the default convolution filter from stats::filter.

Value

Vector of the same length as x with the moving average at each index. Note window - 1 of the values will be NA, and depending on the align parameter these missing values will appear either at the beginning, end, or both sides of the vector.

Examples


x <- rpois(100, lambda = 3)
mav(x, window = 7, align = "center")
mav(x, window = 7, align = "left")

vpnagraj/yawp documentation built on March 31, 2022, 9:56 a.m.