shiftCols: Shift columns to create lags, leads or differences

View source: R/shiftCols.R

shiftColsR Documentation

Shift columns to create lags, leads or differences

Description

Create n-lagged/leaded/differenced columns in a data frame with automatic names.

Usage

shiftCols(data, panel, type = c("lag", "lead", "difference"), shifts, columns)

Arguments

data

data of class data.frame

panel

the panel or cross-section variable name in the data

type

shift type. either "lag", "lead" or "difference"

shifts

the number of lags, leads or differences

columns

the columns in 'data' to be lagged, leaded or differenced

Value

data.frame / data.table with new variables

Note

The function will automatically coerce the data.frame object to a data.table object. (data.table objects retain their data.frame class.)

Author(s)

Lawrence R De Geest, lrdegeest@gmail.com

Examples

# Using the hours and wages data provided by the panelGMM package:
data("hours_wages")
# shiftCols() is designed to work nicely with the pipe operator `%>%` from `magrittr`:
hours_wages_gmm = hours_wages %>%
   shiftCols(data = ., panel=id, type = "difference", shifts = 1, columns=c("lnhr", "lnwg", "kids", "age", "agesq", "disab")) %>%
   shiftCols(data = ., panel=id, type = "lag", shifts = 1:4, columns=c("lnhr", "lnwg", "kids", "age", "agesq", "disab"))

lrdegeest/panelGMM documentation built on Feb. 13, 2025, 12:12 a.m.