Hmisc (version 2.0-9)

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.

Usage

Lag(x, shift = 1)

Arguments

x
a vector
shift
positive integer specifying the number of observations to be shifted to the right

Value

  • a vector like x

Details

A.ttributes of the original object are carried along to the new lagged one, but factor vectors are converted to character.

See Also

lag

Examples

Run this code
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)

Run the code above in your browser using DataCamp Workspace