Lag.Rd
Shifts a vector shift
elements later. Character or factor
variables are padded with ""
, numerics with NA
. The shift
may be negative.
Lag(x, shift = 1)
A.ttributes of the original object are carried along to the new lagged one.
a vector like x
Lag(1:5,2)
#> [1] NA NA 1 2 3
Lag(letters[1:4],2)
#> [1] "" "" "a" "b"
Lag(factor(letters[1:4]),-2)
#> [1] c d <NA> <NA>
#> Levels: a b c d
# Find which observations are the first for a given subject
id <- c('a','a','b','b','b','c')
id != Lag(id)
#> [1] TRUE FALSE TRUE FALSE FALSE TRUE
!duplicated(id)
#> [1] TRUE FALSE TRUE FALSE FALSE TRUE