Learn R Programming

SciencesPo (version 1.02.12)

shift: Lag or Lead Observations

Description

Shift function allows one to either lag or lead a column variables in a data frame.

Usage

shift(x, id, time, delta = 1)

Arguments

x
the variable to be lagged or leaded
id
the subject or identification variable.
time
the time id variable.
delta
an integer value (positive or negative) for units to move either backward or forward.

Value

  • An object of the same type as x

encoding

UTF-8

Details

The combination of id and time must yelds to a unique identification of the observations.

Examples

Run this code
data(sheston91)
# lag
sheston91$L.pop <- with(sheston91, shift(x = pop, id = country, time = year, delta = 1) )
head(sheston91)
# lead
 sheston91$pop.L <- with(sheston91, shift(x = pop, id = country, time = year, delta =  -1) )
 head(sheston91)

Run the code above in your browser using DataLab