Hello! I have a data frame A (below) with a grouping factor (group). I take my DV and create the new, lagged DV by applying the function lag.it (below). It works fine.
A <- data.frame(year=rep(c(1980:1984),3), group= factor(sort(rep(1:3,5))), DV=c(rnorm(15))) lag.it <- function(x) { DV <- ts(x$DV, start = x$year[1]) idx <- seq(length = length(DV)) DVs <- cbind(DV, lag(DV, -1))[idx,] out<-cbind(x, DVs[,2]) # wages[,2] names(out)[length(out)]<-"DV.lag" return(out) } A A.lagged <- do.call("rbind", by(A, A$group, lag.it)) A.lagged Now, I am trying to create the oppostive of lag for DV (should I call it "lead"?) I tried exactly the same as above, but with a different number under lag function (below), but it's not working. I am clearly doing something wrong. Any advice? Thanks a lot! lead.it <- function(x) { DV <- ts(x$DV, start = x$year[1]) idx <- seq(length = length(DV)) DVs <- cbind(DV, lag(DV, 2))[idx,] out<-cbind(x, DVs[,2]) names(out)[length(out)]<-"DV.lead" return(out) } A A.lead <- do.call("rbind", by(A, A$group, lead.it)) A.lead -- Dimitri Liakhovitski Ninah Consulting www.ninah.com ______________________________________________ R-help@r-project.org 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.