Analogous function for lead
and lag
in dplyr by
wrapping data.table's shift
.
lead_dt(x, n = 1L, fill = NA) lag_dt(x, n = 1L, fill = NA)
x | A vector |
---|---|
n | a positive integer of length 1, giving the number of positions to lead or lag by. Default uses 1 |
fill | Value to use for padding when the window goes beyond the input length.
Default uses |
A vector
lead_dt(1:5)#> [1] 2 3 4 5 NAlag_dt(1:5)#> [1] NA 1 2 3 4lead_dt(1:5,2)#> [1] 3 4 5 NA NAlead_dt(1:5,n = 2,fill = 0)#> [1] 3 4 5 0 0