glag {gfunctions} | R Documentation |
Lag a vector or a matrix, with special treatment of zoo
and ts
objects
Description
The glag()
function is similar to the lag()
function from the stats package, but glag()
actually lags (the default in lag()
is to lead). The funtion glag()
also enables padding (for example NA
s or 0
s) of the lost entries. Contrary to the lag()
function, however, the default in glag()
is to pad (with NA
s). The glag()
is particularly suited for zoo
objects, since their indexing is retained. The prefix g
is a reminder of who to blame if things do not work properly.
Usage
## generic:
glag(x, ...)
## Default S3 method:
glag(x, k = 1, pad = TRUE, pad.value = NA, ...)
Arguments
x |
|
k |
integer equal to the lag (the default is 1). Negative values (that is, 'leading') is not possible. |
pad |
logical. If |
pad.value |
the padding value. |
... |
additional arguments |
Value
A vector or matrix, or objects of class zoo
or ts
, with the lagged values.
Author(s)
Genaro Sucarrat, http://www.sucarrat.net/
See Also
Examples
##lag series with NA for the missing entries:
x <- rnorm(5)
glag(x)
##lag series with no padding:
x <- rnorm(5)
glag(x, pad = FALSE)
##lag series and retain the original zoo-index ordering:
x <- as.zoo(rnorm(5))
glag(x)
##lag two periods:
glag(x, k = 2)