Skip to content

Instantly share code, notes, and snippets.

@drsimonj
Last active July 8, 2022 14:53
Show Gist options
  • Save drsimonj/2038ff9f9c67063f384f10fac95de566 to your computer and use it in GitHub Desktop.
Save drsimonj/2038ff9f9c67063f384f10fac95de566 to your computer and use it in GitHub Desktop.
Example of creating multiple lags with dplyr
library(dplyr)
d <- data_frame(x = seq_len(100))
d
#> # A tibble: 100 x 1
#> x
#> <int>
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5
#> 6 6
#> 7 7
#> 8 8
#> 9 9
#> 10 10
#> # ... with 90 more rows
lags <- seq(10)
lag_names <- paste("lag", formatC(lags, width = nchar(max(lags)), flag = "0"),
sep = "_")
lag_functions <- setNames(paste("dplyr::lag(., ", lags, ")"), lag_names)
d %>% mutate_at(vars(x), funs_(lag_functions))
#> # A tibble: 100 x 11
#> x lag_01 lag_02 lag_03 lag_04 lag_05 lag_06 lag_07 lag_08 lag_09
#> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1 1 NA NA NA NA NA NA NA NA NA
#> 2 2 1 NA NA NA NA NA NA NA NA
#> 3 3 2 1 NA NA NA NA NA NA NA
#> 4 4 3 2 1 NA NA NA NA NA NA
#> 5 5 4 3 2 1 NA NA NA NA NA
#> 6 6 5 4 3 2 1 NA NA NA NA
#> 7 7 6 5 4 3 2 1 NA NA NA
#> 8 8 7 6 5 4 3 2 1 NA NA
#> 9 9 8 7 6 5 4 3 2 1 NA
#> 10 10 9 8 7 6 5 4 3 2 1
#> # ... with 90 more rows, and 1 more variables: lag_10 <int>
@brshallo
Copy link

@RJHKnight
Copy link

Here is another alternative solution using across, which gives a little more flexibility in selecting which columns to lag.

https://gist.github.com/RJHKnight/22dbe5a3ef1d2701afd48370a1f1742c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment