Create Lags – R

Creating lags in R isnt very difficult, but if you want to create a number of lag or moving average fields, it could be pretty tedious.

This code createlags.R will make it easier for you to create any number of lags or moving averages to your dataset

Usage: createlags(dataframe, field, numlags, nummovingavg)

Example: newdata = createlags(df, "total", 4, 6)
# creates 4 lags and 6 period moving averages
# of the total field in df

Download the file: createlags.R