Dplyr Multiple Lags Tidy Eval?
问题 I am trying to make multiple lags using the least amount of code possible in dplyr, while sticking to tidy eval. The following Standard Evaluation (SE) code works: #if(!require(dplyr)) install.packages("dplyr"); library(dplyr) a=as_tibble(c(1:100)) lags=3 lag_prefix=paste0("L", 1:lags, ".y") multi_lag=setNames(paste("lag(.,", 1:lags, ")"), lag_prefix) a %>% mutate_at(vars(value), funs_(multi_lag)) #final line # A tibble: 100 x 4 value L1.y L2.y L3.y <int> <int> <int> <int> 1 1 NA NA NA 2 2 1