leadx-lagx: Compute lagged or leading values

Description Usage Arguments Details Value Examples

Description

Stable lifecycle

Find the "previous" (lagx()) or "next" (leadx()) values in a vector. Useful for comparing values behind of or ahead of the current values.

Usage

1
2
3
lagx(x, n = 1L, fill = NA)

leadx(x, n = 1L, fill = NA)

Arguments

x

[univariate vector]

Univariate vector, numeric or ts object with only one dimension.

n

[positive integer(1): 1L]

Value indicating the number of positions to lead or lag by.

fill

[numeric or function: NA]

Numeric value(s) or function used to fill observations.

Details

This functions has been taken and modified from the dplyr package, however, to reduce dependencies they are not imported.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

1
2
3
4
5
6
7
x <- c(5,3,2,2,5)
lagx(x)
lagx(x, fill = mean)
lagx(x, fill = fill_nocb)

leadx(x)
leadx(x, fill = fill_locf)

kvasilopoulos/transx documentation built on Jan. 26, 2021, 6:14 p.m.