R: Lag a vector or a matrix, with special treatment of zoo...
glag
R Documentation
Lag a vector or a matrix, with special treatment of zoo objects
Description
Similar to the lag function from the stats package, but glag enables padding (e.g. NAs or 0s) of the lost entries. Contrary to the lag function in the stats package, however, the default in glag is to pad (with NAs). The glag is particularly suited for zoo objects, since their indexing is retained
Usage
glag(x, k = 1, pad = TRUE, pad.value = NA)
Arguments
x
a numeric vector or matrix
k
integer equal to the lag (the default is 1)
pad
logical. If TRUE (default), then the lost entries are padded with pad.value. If FALSE, then no padding is undertaken
##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)