Learn R Programming

statforbiology (version 0.9.9)

ma: 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)

Value

This function returns a vector of moving averages

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

Author

Andrea Onofri

Examples

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

Run the code above in your browser using DataLab