Find the "previous" (lagx()
) or "next" (leadx()
) values in a vector.
Useful for comparing values behind of or ahead of the current values.
lagx(x, n = 1L, fill = NA) leadx(x, n = 1L, fill = NA)
x |
Univariate vector, numeric or ts object with only one dimension. |
---|---|
n |
Value indicating the number of positions to lead or lag by. |
fill |
Numeric value(s) or function used to fill observations. |
Returns a vector with the same class and attributes as the input vector.
This functions has been taken and modified from the dplyr
package,
however, to reduce dependencies they are not imported.
#> [1] NA 5 3 2 2lagx(x, fill = mean)#> [1] 3 5 3 2 2lagx(x, fill = fill_nocb)#> [1] 5 5 3 2 2leadx(x)#> [1] 3 2 2 5 NAleadx(x, fill = fill_locf)#> [1] 3 2 2 5 5