There is a Lag function in Hmisc and I found this on StackExchange
shift <- function (x, shift_by) { #similar to lag function
stopifnot(is.numeric(shift_by))
stopifnot(is.numeric(x))
if (length(shift_by)>1)
return(sapply(shift_by,shift, x=x))
out<-NULL
abs_shift_by=abs(shift_by)
if (shift_by > 0 )
out<-c(tail(x,-abs_shift_by),rep(NA,abs_shift_by))
else if (shift_by < 0 )
out<-c(rep(NA,abs_shift_by), head(x,-abs_shift_by))
else
out<-x
out
}
-----Original Message-----
From:
[hidden email] [mailto:
[hidden email]]
On Behalf Of saraberta
Sent: Wednesday, July 25, 2012 4:10 AM
To:
[hidden email]
Subject: [R] lagged variables
hi guys,
i have some trouble in creating lagged variables to use as external
regressors.
i'm trying to use lag(x) but it gives me as result the same time series
(x), adding this part at the end:
attr(,"tsp")
[1] 0 2323 1
where do i wrong?are there other functions to be used?
thanks
sara
--
View this message in context:
http://r.789695.n4.nabble.com/lagged-variables-tp4637734.htmlSent from the R help mailing list archive at Nabble.com.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide
http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide
http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.