[R] Efficient way of creating a shifted (lagged) variable?

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Thu Aug 4 17:24:24 CEST 2011


Hello!

I have a data set:
set.seed(123)
y<-data.frame(week=seq(as.Date("2010-01-03"), as.Date("2011-01-31"),by="week"))
y$var1<-c(1,2,3,round(rnorm(54),1))
y$var2<-c(10,20,30,round(rnorm(54),1))

# All I need is to create lagged variables for var1 and var2. I looked
around a bit and found several ways of doing it. They all seem quite
complicated - while in SPSS it's just a few letters (like LAG()). Here
is what I've written but I wonder. It works - but maybe there is a
very simple way of doing it in R that I could not find?
I need the same for "lead" (opposite of lag).
Any hint is greatly appreciated!

### The function I created:
mylag <- function(x,max.lag=1){   # x has to be a 1-column data frame
   temp<-as.data.frame(embed(c(rep(NA,max.lag),x[[1]]),max.lag+1))[2:(max.lag+1)]
   for(i in 1:length(temp)){
     names(temp)[i]<-paste(names(x),".lag",i,sep="")
    }
  return(temp)
}

### Running mylag to get my result:
myvars<-c("var1","var2")
for(i in myvars) {
  y<-cbind(y,mylag(y[i]),max.lag=2)
}
(y)

-- 
Dimitri Liakhovitski
marketfusionanalytics.com



More information about the R-help mailing list