Skip to content

Instantly share code, notes, and snippets.

@drsimonj
Last active July 8, 2022 14:53
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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>
@Fredo-XVII
Copy link

Hello,

This code was brilliant! I had seen setNames() used before, but I didn't realize what it was doing, and I actually still can't wrap my head around how it creates the column names. Now I can stick to tidyverse dependencies and I don't have to depend on tidyquant to create lags of my variables, although I did add glue as a dependency to make the pasting more clear. I also wrapped the tsibble::difference() into a function using the same idea.

Thanks, this was great!!

@mpettis
Copy link

mpettis commented Dec 1, 2020

This was great! As funs_() is now deprecated, I had to do some reading up, and I came up with the following as a possible alternative without using funs_(): https://gist.github.com/mpettis/c4a4e930e6e0d69b25249484378e9f5f

The hard part for me is the string -> function part, which works for functions that already have names, but parsing a string specifying a function was hard -- lambda function parsing worked for me.

@LiangChen3519
Copy link

Thank you so much. Your code helps me a lot.

@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