esm_lag: Lag observation-level variables

Description Usage Arguments Details

Description

This function will lag any variable on the observation level. It takes a dataset, a character vector of variables, and a character vector of grouping variables (by default the person and day ids) and lags within these groups. With the default grouping levels, this will allow lags within day, but will not allow lags across day (the first lagged observation of each day for each P will always be NA).

Usage

1
esm_lag(data, variables, person_id, day_id, order = "obs_id")

Arguments

data

The dataset to operate on.

variables

The character vector of variables to lag

person_id

The person level grouping variable - this should be your person id variable.

day_id

The day level grouping variable - this should be your day id variable.

order

The observation number to order by within days. By default it is 'obs_id'. You will want this to be a unique, ordered observation identifier. It does not matter whether it is unique only within days (i.e. resets each day for each p).

Details

It can't be used for lagging day level variables because those need to be aggregated or sliced to one obs per day first - the dplyr lag function this is built on doesn't lag at the grouping level, it just lags within groups by 1 row.

esm_lag and esm_day_lag are unique in that they output variables with the same appelation (_lag) because they output what we would expect for lag at each level so this intuitively makes sense. The variables will already have names to say whether they're at the day level or not.


seanchrismurphy/emacalc documentation built on May 12, 2019, 2:03 p.m.