[R] lags of a variable, with a factor

Jim Lemon jim at bitwrit.com.au
Mon Aug 26 23:34:06 CEST 2013


On 08/26/2013 11:44 PM, Michael Friendly wrote:
> ...
> Thanks for trying again, but that doesn't work either with a by=
> variable. Note that your function is recursive, and also k=k
> should be passed in the else{ ... lags() }.
>
Hi Michael,
You are correct about the k=, and I had used a separate object for the 
by= argument which did work. So, I tried to work around the problem of 
having the by= argument look like an object with deparse(substitute()) 
but couldn't get it to go. However, if you can live with passing the 
factor name as a string, this seems to work.

lags<-function(x,k=1,prefix='lag',by) {
  if(missing(by)) {
   n<-length(x)
   res<-data.frame(lag0=x)
   for(i in 1:k) res<-cbind(res,c(rep(NA,i),x[1:(n-i)]))
   colnames(res)<-paste0(prefix,0:k)
  }
  else {
   byfac<-factor(unlist(x[by]))
   x<-x[!(names(x) %in% by)]
   for(levl in levels(byfac)) {
    nextlags<-lags(x[byfac==levl,],k=k,prefix=prefix)
    rownames(nextlags)<-paste(levl,rownames(nextlags),sep=".")
    if(exists("res")) res<-rbind(res,nextlags)
    else res<-nextlags
   }
  }
  return(res)
}

lags(events2,3,by="sub")
     lag0 lag1 lag2 lag3
1.1    b <NA> <NA> <NA>
1.2    c    b <NA> <NA>
1.3    c    c    b <NA>
1.4    d    c    c    b
1.5    c    d    c    c
2.1    d <NA> <NA> <NA>
2.2    c    d <NA> <NA>
2.3    d    c    d <NA>
2.4    a    d    c    d
2.5    a    a    d    c


Jim



More information about the R-help mailing list