Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

simple.lag

applies function to moving subsets of a data vector


Description

Used to apply a function to subsets of a data vector. In particular, it is used to find moving averages over a certain "lag" period.

Usage

simple.lag(x, lag, FUN = mean)

Arguments

x

a data vector

lag

the lag amount to use.

FUN

a function to apply to the lagged data. Defaults to mean

Details

The function FUN is applied to the data x[(i-lag):i] and assigned to the (i-lag)th component of the return vector. Useful for finding moving averages.

Value

returns a vector.

Author(s)

Provided to R help list by Martyn Plummer

See Also

filter

Examples

## find a moving average of the dow daily High
data(dowdata)
lag = 50; n = length(dowdata$High)
plot(simple.lag(dowdata$High,lag),type="l")
lines(dowdata$High[lag:n])

UsingR

Data Sets, Etc. for the Text "Using R for Introductory Statistics", Second Edition

v2.0-6
GPL (>= 2)
Authors
John Verzani <verzani@math.csi.cuny.edu>
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.