Creating lags for zoo facilities

I create a lag for zoo objects using the following econ$gdp4 <- lag(econ$gdp, k = -4, na.pad = TRUE) . I have about 6 columns in an econ object for which I want to create lags, and I want to create a lag for periods 1 to 9. Is there a way to use a loop to create them?

+4
source share
1 answer

Suppose z is our zoo object. Suppose we need 9 lags for each column 2, 3, 4, as well as all columns of the original. Then try:

 merge(z, lag(z[, 2:4], -(1:9))) 

Also note that lagging from 0 returns the same column, so this gives both the original and 9 lags of each column:

 lag(z, -(0:9)) 
+6
source

Source: https://habr.com/ru/post/1387088/


All Articles