View source: R/preprocessing.R
lagVarsNested | R Documentation |
Creates extra column(s) with lagged variables according to the order of the
data.frame. The argument nestVars
describes with regards to which
variables the data is nested. The function then creates lagged variables only
within each group of the nestVars
. This is particularly useful when
working with multilevel data, where observations are nested, e.g., within
individuals.
lagVarsNested(
dat = dat,
vars,
nestVars,
lags = 1,
diffvars = NULL,
unit = "secs",
verbose = T
)
dat |
data.frame containing the variables to be lagged |
vars |
names of the columns in the data.frame that should be lagged |
nestVars |
name(s) of the columns indicating how the data is nested
(e.g., ID variable). Currently up to two |
lags |
vector defining the |
diffvars |
names of columns in the data.frame of which the difference to the current row should be taken (this is particularly helpful for dealing with time variables and time differences) |
unit |
character value, if time variables are being lagged and the difference is taken, which time unit should be used to calculate the difference |
verbose |
boolean indicating about the state of the process be displayed |
data.frame with new columns containing the lagged variables
lagVars
dat <- data.frame(ID = c(rep(1,10), rep(2,10)),
day = rep(c(rep(1,5), rep(2,5)),2),
a = runif(20, max = 10),
b = Sys.Date()+1:20,
cat = sample(c("X","Y","Z"), 20, replace = T))
lagVarsNested(dat, lags = 1:2, nestVars = c("ID","day"),
vars = c("a","b"), diffvars = "b", unit = "days")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.