[R] lag a data.frame column?

Angel Spassov anspassov at googlemail.com
Thu Sep 10 14:28:28 CEST 2009


Mark Knecht wrote (09.Sep.2009 at 10:43 -0700):
> Sometimes it's the simple things...
> 
> Why doesn't this lag X$x by 3 and place it in X$x1? (i.e. - Na's in
> the first 3 rows and then values showing up...)
> 
> The help page does talk about time series. If lag doesn't work on
> data.frame columns then what would be the right function to use to lag
> by a variable amount?
> 
> Thanks,
> Mark
> 
> 
> X=data.frame(x=seq(1:10))
> X$x1=lag(X$x, 3)
> X
> 

I think the embed function might be helpful for you, 

embed(1:10 , dimension = 3)

Best,
AS




More information about the R-help mailing list