mlag_dfr: Lag multiple columns of a data frame

mlag_dfr(df, col_names, lags)

Arguments

df

The dataframe containing the time series data

col_names

A vector of column names to be used to create the lagged variables

lags

Number of lags to create for the column

Examples

x <- c(2,3,5,7,9,9,5,3) z <- c(6,9,3,3,3,4,5,7) y <- 1:8 df <- data.frame(Y = y, X = x, Z =z) lag_dfr(df, "X", 1:3)
#> X_1 X_2 X_3 #> 1 2 2 2 #> 2 3 3 3 #> 3 5 5 5 #> 4 7 7 7 #> 5 9 9 9 #> 6 9 9 9 #> 7 5 5 5 #> 8 3 3 3
mlag_dfr(df, c("X","Z"), 1:3)
#> Y X Z X_1 X_2 X_3 Z_1 Z_2 Z_3 #> 1 1 2 6 2 2 2 6 6 6 #> 2 2 3 9 3 3 3 9 9 9 #> 3 3 5 3 5 5 5 3 3 3 #> 4 4 7 3 7 7 7 3 3 3 #> 5 5 9 3 9 9 9 3 3 3 #> 6 6 9 4 9 9 9 4 4 4 #> 7 7 5 5 5 5 5 5 5 5 #> 8 8 3 7 3 3 3 7 7 7