lag.zoo.RdMethods for computing lags and differences of "zoo" objects.
a "zoo" object.
For lag the number of lags (in units of observations).
Note the sign of k behaves as in lag.
For diff it is the number of backward lags used (or if negative the
number of forward lags.
an integer indicating the order of the difference.
logical. Should arithmetic (or geometric) differences be computed?
logical. If TRUE it adds any times that would not otherwise have been in
the result with a value of NA. If FALSE those times are dropped.
logical. Should the differences of the log series be computed?
currently not used.
These methods for "zoo" objects behave analogously to the default
methods. The only additional arguments are arithmetic and log in diff
and na.pad in both lag.zoo and diff.zoo.
Also, "k" can be a vector of lags in which case the names of
"k", if any, are used in naming the result.
The lagged or differenced "zoo" object.
Note the sign of k: a series lagged by a positive k
is shifted earlier in time.
lag.zoo and lag.zooreg can give different results.
For a lag of 1 lag.zoo moves points to the adjacent time point
whereas lag.zooreg moves the time by deltat. This
implies that a point in a zoo series cannot be lagged to a time
point that is not already in the series whereas this is possible for
a zooreg series.
x <- zoo(11:21)
lag(x, k = 1)
#> 1 2 3 4 5 6 7 8 9 10
#> 12 13 14 15 16 17 18 19 20 21
lag(x, k = -1)
#> 2 3 4 5 6 7 8 9 10 11
#> 11 12 13 14 15 16 17 18 19 20
# this pairs each value of x with the next or future value
merge(x, lag1 = lag(x, k=1))
#> x lag1
#> 1 11 12
#> 2 12 13
#> 3 13 14
#> 4 14 15
#> 5 15 16
#> 6 16 17
#> 7 17 18
#> 8 18 19
#> 9 19 20
#> 10 20 21
#> 11 21 NA
diff(x^3)
#> 2 3 4 5 6 7 8 9 10 11
#> 397 469 547 631 721 817 919 1027 1141 1261
diff(x^3, -1)
#> 1 2 3 4 5 6 7 8 9 10
#> 397 469 547 631 721 817 919 1027 1141 1261
diff(x^3, na.pad = TRUE)
#> 1 2 3 4 5 6 7 8 9 10 11
#> NA 397 469 547 631 721 817 919 1027 1141 1261