R/fwdDiff.R

Defines functions fwdDiff

Documented in fwdDiff

#'
#'  Forward difference of a time series
#'
#' @param x zoo or xts matrix
#' @param lag numeric; period to difference over
#' @param na.pad logical
#' @return A matrix (same type as input) of forward differences
#'
#' @export
#'
fwdDiff = function(x, lag, na.pad=TRUE) {
  ensure(x, is.zoo)
  ensure(lag, is.numeric)
  ensure(na.pad, is.logical)

  stopifnot(lag >= 0)

  (xts::diff.xts(x, lag=lag)
   %>% xts::lag.xts(k=-(lag)) )
}
pteetor/tutils documentation built on April 25, 2024, 9:14 a.m.