powered by
Generates lagged (or lead) versions of selected numeric columns. Positive lags shift values backward in time (past values), negative lags shift forward. Useful for time series modeling.
create_lags(data, cols = NULL, lags = 1, prefix = "lag_", group = NULL, date_col = NULL, verbose = FALSE)
A data frame or matrix with additional lag columns.
A data frame, matrix, or numeric vector.
Column indices or names to create lags for.
Vector of lag orders (positive for past, negative for future).
Prefix for new column names.
Optional grouping column for group-specific lags.
Currently ignored. Data are assumed to be in the correct time order.
Logical; if TRUE, prints number of lag columns created.
TRUE
create_lags(data[1:100, c(1, 4, 17:19)], cols = 3:5, lags = c(1, 2))
Run the code above in your browser using DataLab