lag | R Documentation |
Find the "previous" (lag()
) or "next" (lead()
) values in a vector. Useful for comparing values behind of or ahead
of the current values.
lag(x, n = 1L, default = NA)
lead(x, n = 1L, default = NA)
x |
A |
n |
A positive |
default |
The value used for non-existent rows (default: |
lag(1:5)
lead(1:5)
x <- 1:5
data.frame(behind = lag(x), x, ahead = lead(x))
# If you want to look more rows behind or ahead, use `n`
lag(1:5, n = 1)
lag(1:5, n = 2)
lead(1:5, n = 1)
lead(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)
lead(1:5)
lead(1:5, default = 6)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.