lag.zoo supports vector-based lags on zoo objects.
A few caveats:
- dplyr's lag clobbers the base R lag (which you need to
invoke lag's methods) so if you have dplyr loaded be sure
to refer to stats::lag.
- dplyr's lag works backwards relative to the standard set
in base R so dplyr::lag(x, 1) corresponds to stat::lag(x, -1) in
base R
- zoo follows base R's standard
- you can use as.data.frame or fortify.zoo to convert a zoo
object to a data frame if you need that. The first one
drops the time index and the second one includes it.
library(zoo)
stats::lag(zoo(d2$x1), 0:-2)
giving this zoo object:
lag0 lag-1 lag-2
1 1 NA NA
2 2 1 NA
3 3 2 1
4 4 3 2
5 5 4 3
6 6 5 4
7 7 6 5
8 8 7 6
9 9 8 7
10 10 9 8
On Tue, Apr 23, 2019 at 9:10 AM Lorenzo Isella <lorenzo.isella at
gmail.com> wrote:>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com