[R] The opposite of "lag"

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Wed Jul 21 19:16:12 CEST 2010


I reinstalled zoo and now I can see the years on left (raw names).
Before - I could not see them.
Thank you!

On Wed, Jul 21, 2010 at 12:51 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Wed, Jul 21, 2010 at 12:18 PM, Dimitri Liakhovitski
> <dimitri.liakhovitski at gmail.com> wrote:
>> Sorry, I don't think it's working.
>> the last 3 columns (on the right) of result contain the original data
>> of each group.
>> But there is no shift at all. I am trying to reach the following
>> result for each group: The first number disappears and at the bottom
>> an NA appears.
>> Is it possible?
>>
>
> It works for me.  Here is the result.  As we see
>
> - the first 3 columns are lagged ahead so that they start at 1981
>  which is one year later than the original columns start,
> - the second set of 3 columns are the originals so they start at 1980
> - the last set of 3 columns are lagged so they start at 1979 which is
>  one year before the original columns start.
>
>> set.seed(123)
>> A <- data.frame(year=rep(c(1980:1984),3), group=
> + factor(sort(rep(1:3,5))), DV=c(rnorm(15)))
>> library(zoo)
>> z <- read.zoo(A, index = 1, split = "group", frequency = 1)
>> z <- as.zooreg(z) ###
>> lag(z, c(-1, 0, 1))
>        X1.lag.1   X2.lag.1   X3.lag.1     X1.lag0    X2.lag0    X3.lag0
> 1979          NA         NA         NA          NA         NA         NA
> 1980          NA         NA         NA -0.56047565  1.7150650  1.2240818
> 1981 -0.56047565  1.7150650  1.2240818 -0.23017749  0.4609162  0.3598138
> 1982 -0.23017749  0.4609162  0.3598138  1.55870831 -1.2650612  0.4007715
> 1983  1.55870831 -1.2650612  0.4007715  0.07050839 -0.6868529  0.1106827
> 1984  0.07050839 -0.6868529  0.1106827  0.12928774 -0.4456620 -0.5558411
> 1985  0.12928774 -0.4456620 -0.5558411          NA         NA         NA
>         X1.lag1    X2.lag1    X3.lag1
> 1979 -0.56047565  1.7150650  1.2240818
> 1980 -0.23017749  0.4609162  0.3598138
> 1981  1.55870831 -1.2650612  0.4007715
> 1982  0.07050839 -0.6868529  0.1106827
> 1983  0.12928774 -0.4456620 -0.5558411
> 1984          NA         NA         NA
> 1985          NA         NA         NA
>



-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com



More information about the R-help mailing list