Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

Lag

Lag a Numeric, Character, or Factor Vector


Description

Shifts a vector shift elements later. Character or factor variables are padded with "", numerics with NA. The shift may be negative.

Usage

Lag(x, shift = 1)

Arguments

x

a vector

shift

integer specifying the number of observations to be shifted to the right. Negative values imply shifts to the left.

Details

A.ttributes of the original object are carried along to the new lagged one.

Value

a vector like x

Author(s)

Frank Harrell

See Also

Examples

Lag(1:5,2)
Lag(letters[1:4],2)
Lag(factor(letters[1:4]),-2)
# Find which observations are the first for a given subject
id <- c('a','a','b','b','b','c')
id != Lag(id)
!duplicated(id)

Hmisc

Harrell Miscellaneous

v4.5-0
GPL (>= 2)
Authors
Frank E Harrell Jr <fh@fharrell.com>, with contributions from Charles Dupont and many others.
Initial release
2021-02-27

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.