R/cv.diff.R

Defines functions cv.diff

Documented in cv.diff

###############################################################################################
##### Calculating difference in a time series vector
###############################################################################################

#' @name cv.diff
#' @aliases cv.diff
#' @title Calculating the difference of a time series
#' @description Calculate the difference of a time series, with a specific lag period. The difference is used to show the
#' change in value over set period.
#' @usage cv.diff(x,n)
#' @param x : a numeric vector
#' @param n : number of lag periods
#' @examples cv.diff(mtcars[,2],1)


cv.diff <- function(x, n){

  d <- c(as.numeric("NA"), (lag(as.ts(x), k = n)-as.ts(x)))

  return(d)

}

Try the YRmisc package in your browser

Any scripts or data that you put into this service are public.

YRmisc documentation built on March 25, 2020, 5:13 p.m.