Reputation: 55
I have been creating lags for zoo objects using the following econ$gdp4 <- lag(econ$gdp, k = -4, na.pad = TRUE)
. I have about 6 columns in econ
object that I want to create lags for and I want to create lags for periods 1 through 9. Is there a way to use a loop to create these?
Upvotes: 4
Views: 1713
Reputation: 269491
Suppose z is our zoo object. Lets say we want 9 lags for each of columns 2, 3, 4 as well as all columns of the original. Then try:
merge(z, lag(z[, 2:4], -(1:9)))
Also note that a lag of 0 gives back the same column so this gives the original as well as 9 lags of each column:
lag(z, -(0:9))
Upvotes: 7