-
-
Save drsimonj/2038ff9f9c67063f384f10fac95de566 to your computer and use it in GitHub Desktop.
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> |
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.
Thank you so much. Your code helps me a lot.
See my example doing similar here: https://gist.github.com/brshallo/cadaa40cef6387e28924b6c3756627c9
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
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 thetsibble::difference()
into a function using the same idea.Thanks, this was great!!