[R] Multiple Lags with Dplyr

Lorenzo Isella |orenzo@|@e||@ @end|ng |rom gm@||@com
Tue Apr 23 15:10:31 CEST 2019


Dear All,
I refer to the excellent post at

https://purrple.cat/blog/2018/03/02/multiple-lags-with-tidy-evaluation/

What I want to do is to create a function capable, à la dplyr, to
generate new columns which are a lagged version of existing columns in
a data frame.
For instance, you can do this manually as


d2 <- tibble(x1 =1:10, x2=10:19,  x3=50:59)


d3 <- d2%>%mutate(x1lag1=lag(x1, 1), x1lag2=lag(x1,2))


but this becomes quickly tedious when you need to take several lags of
different columns.
One solution in the link above is the following


lags <- function(var, n=10){
  var <- enquo(var)
  
  indices <- seq_len(n)
  map( indices, ~quo(lag(!!var, !!.x)) ) %>% 
    set_names(sprintf("lag_%s_%02d", quo_text(var), indices))
  
}


d4 <- d2 %>% 
  mutate( !!!lags(x1, 3), !!!lags(x2,3) )


does anybody know how this could be made more general? I mean that I
would like to take a fixed number of lags of a list of columns (x1 and
x2, for instance), just by passing the list of columns and without
repeating the commands for x1 and x2.
Any suggestion is appreciated.
Cheers

Lorenzo



More information about the R-help mailing list