[R] lead

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 12 16:40:47 CEST 2007


The lag.zoo method of lag in the zoo package supports positive, negative
and multiple lags and has an na.pad= argument.  (zoo also has a
lag.zooreg method, not shown, for zooreg objects):

> library(zoo)
> z <- zoo(11:15)
> z
 1  2  3  4  5
11 12 13 14 15
> lag(z, na.pad = TRUE)
 1  2  3  4  5
12 13 14 15 NA
> lag(z, 1, na.pad = TRUE) # same
 1  2  3  4  5
12 13 14 15 NA
>
> # negative lag
> lag(z, -1, na.pad = TRUE)
 1  2  3  4  5
NA 11 12 13 14
>
> # mulitple lags
> lag(z, 1:3, na.pad = TRUE)
  lag1 lag2 lag3
1   12   13   14
2   13   14   15
3   14   15   NA
4   15   NA   NA
> lag(z, -(1:3), na.pad = TRUE)
  lag-1 lag-2 lag-3
2    11    NA    NA
3    12    11    NA
4    13    12    11
5    14    13    12

vignette("zoo") # more info on zoo


On 7/12/07, Aydemir, Zava (FID) <Zava.Aydemir at morganstanley.com> wrote:
> Hi,
>
> is there any function in R that shifts elements of a vector to the
> opposite direction of what Lag()  of the Hmisc package does? (something
> like, Lag(x, shift = -1) )
>
> Thanks
>
> Zava
> --------------------------------------------------------
>
> This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> 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.
>



More information about the R-help mailing list