ma: Moving average for a vector

View source: R/serviceFunctions.R

maR Documentation

Moving average for a vector

Description

This is a wrapper for the 'filter()' function that uses the convolution method to calculate the moving averages of the terms in a vector, i.e. a series of averages of different subsets of the full vector

Usage

ma(x, n = 5, sides = 2)

Arguments

x

a numeric vector representing a time series

n

an integer, representing the number of values that compose each subset to be averaged

sides

can be either 1 or 2. If sides = 1 the n values to be averaged are taken before the preent values; If sides = 2 the n values are taken before and after the present value; If n is odd, (n - 1)/2 values are taken before the present value and (n - 1)/2 are taken after the present value, while, if n is even, more values are taken after the present value

Value

This function returns a vector of moving averages

Author(s)

Andrea Onofri

Examples

series <- c(319, 317, 332, 271, 301, 292, 351, 358, 259, 270)
ma(series, n = 4, sides = 2)


statforbiology documentation built on Oct. 30, 2024, 9:13 a.m.