[R] lags of a variable, with a factor
Jim Lemon
jim at bitwrit.com.au
Sat Aug 24 12:25:43 CEST 2013
On 08/24/2013 04:16 AM, Michael Friendly wrote:
> For sequential analysis of sequences of events, I want to calculate a
> series of lagged
> versions of a (numeric or character) variable. The simple function below
> does this,
> but I can't see how to generalize this to the case where there is also a
> factor variable
> and I want to calculate lags separately for each level of the factor
> (by). Can anyone help?
> ...
> > do.call(rbind, lg)
> lag0 lag1 lag2
> 1.1 b <NA> <NA>
> 1.2 d b <NA>
> 1.3 d d b
> 1.4 c d d
> 1.5 b c d
> 2.1 b <NA> <NA>
> 2.2 b b <NA>
> 2.3 b b b
> 2.4 d b b
> 2.5 a d b
> >
>
Hi Michael,
Maybe this will do it.
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)
return(res)
}
else {
for(levl in levels(by)) {
nextlags<-lags(x[by==levl,],prefix=prefix)
rownames(nextlags)<-paste(levl,rownames(nextlags),sep=".")
if(exist(res)) res<-rbind(res,nextlags)
else res<-nextlags
}
}
}
Jim
More information about the R-help
mailing list