# NOT RUN {
lead(1:10, 1)
lead(1:10, 2)
lag(1:10, 1)
lead(1:10, 1)
x <- runif(5)
cbind(ahead = lead(x), x, behind = lag(x))
# Use order_by if data not already ordered
df <- data.frame(year = 2000:2005, value = (0:5) ^ 2)
scrambled <- df[sample(nrow(df)), ]
wrong <- mutate(scrambled, prev = lag(value))
arrange(wrong, year)
right <- mutate(scrambled, prev = lag(value, order_by = year))
arrange(right, year)
# }
Run the code above in your browser using DataLab