## Compute lagged or leading values

### Description

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

### Usage

lag(x, n = 1L, default = NA, order_by = NULL, ...)

lead(x, n = 1L, default = NA, order_by = NULL, ...)


### Arguments

 x Vector of values n Positive integer of length 1, giving the number of positions to lead or lag by default Value used for non-existent rows. Defaults to NA. order_by Override the default ordering to use another vector or column ... Needed for compatibility with lag generic.

### Examples

lag(1:5)

x <- 1:5

# If you want to look more rows behind or ahead, use n
lag(1:5, n = 1)
lag(1:5, n = 2)

# If you want to define a value for non-existing rows, use default
lag(1:5)
lag(1:5, default = 0)

# If data are not already ordered, use order_by