Learn R Programming

DataCombine (version 0.1.23)

slideMA: Create a moving average for a period before or after each time point for a given variable

Description

Create a moving average for a period before or after each time point for a given variable

Usage

slideMA(data, Var, GroupVar = NULL, periodBound = -3, offset = 1,
  NewVar = NULL, reminder = TRUE)

Arguments

data
a data frame object.
Var
a character string naming the variable you would like to create the lag/lead moving averages from.
GroupVar
a character string naming the variable grouping the units within which Var will be turned into slid moving averages. If GroupVar = NULL then the whole variable is slid up or down and moving averages will be created. This
periodBound
numeric. The time point for the outer bound of the time period overwhich to create the moving averages. The default is -3, i.e. the period begins three time periods before a given time period. Can also be positive for leading moving a
offset
numeric. How many time increments away from a given increment to begin the moving average time period. The default is 1. Note: must be positive.
NewVar
a character string specifying the name for the new variable to place the slid data in.
reminder
logical. Whether or not to remind you to order your data by the GroupVar and time variable before running slideMA.

Value

  • a data frame

See Also

shift, slide, ddply

Examples

Run this code
# Create dummy data
 A <- B <- C <- 1:20
 ID <- sort(rep(seq(1:4), 5))
 Data <- data.frame(ID, A, B, C)

 # Lead the variable by two time units
 DataSlidMA1 <- slideMA(Data, Var = "A", NewVar = "ALead_MA",
                 periodBound = 3)

 # Lag the variable one time unit by ID group
 DataSlidMA2 <- slideMA(data = Data, Var = "B", GroupVar = "ID",
                NewVar = "BLag_MA", periodBound = -3, offset = 2)

Run the code above in your browser using DataLab