Learn R Programming

tidytable (version 0.10.2)

leads.: Get lagging or leading values

Description

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

Usage

leads.(x, n = 1L, default = NA)

Arguments

x

a vector of values

n

a 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.

Examples

Run this code
x <- 1:5

lag(x, 1)
lead(x, 1)

# Also works inside of `mutate()`
df <- tidytable(x = 1:5)

df %>%
  mutate(lag_x = lag(x))

Run the code above in your browser using DataLab