lag(1:5)
lead(1:5)
x <- 1:5
tibble(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)
# If data are not already ordered, use `order_by`
scrambled <- slice_sample(tibble(year = 2000:2005, value = (0:5) ^ 2), prop = 1)
wrong <- mutate(scrambled, previous_year_value = lag(value))
arrange(wrong, year)
right <- mutate(scrambled, previous_year_value = lag(value, order_by = year))
arrange(right, year)
Run the code above in your browser using DataLab