# NOT RUN {
set.seed(100)
## create a dataset with dates
x <- data.frame(
hospid = 1:100,
docid = round(runif(100, 1, 10)),
dis_date = formatDate(runif(100, 42700, 42800))
)
## lagged dis_date, not specifed "by"
lag(x, dis_date)
# }
# NOT RUN {
## lagged dis_date by docid
## first we need to sort
y <- x[order(x$docid), ]
y
## lag dates within groups
lag(y, dis_date, by = docid, new_var = lag_date)
lag(y, dis_date, by = docid, lag_date, TRUE)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab