panel_lag: Lag panel data

Description Usage Arguments Value Examples

View source: R/panel_lag.R

Description

A function that correctly lags panel data where units are identified by id and time periods are identified with t. Results are in same order as data and are padded with NA as needed.

Usage

1
panel_lag(x, id, t, lag = 1, data = NULL)

Arguments

x

String identifying the vectors to be lagged in data.

id

String identifying the unit (e.g. country) identifier in data.

t

String identifying the time identifier in data.

lag

Lag order, i.e. by how many time periods should x be lagged? Unlike the default lag, positive values indicate that past data is used for the current time period.

data

A data frame. If not provided, a new one will be constructed with the vectors supplied for the other parameters.

Value

A vector of same length as x representing lagged values with leading NA's.

Examples

1
2
3
4
5
6
7
8
data(coups)
# No need to order before using panelLag, just do it here so we can compare results below.
coups <- coups[order(coups$gwcode, coups$year), ]
test <- panel_lag("polity2", "gwcode", "year", data=coups)

# Compare output
head(coups$polity2)
head(test)

spduration documentation built on May 19, 2017, 9:51 a.m.

Search within the spduration package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs in the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.