R/rebase.R

#' Uses CPI data to convert prices from a base year to a desired output year.
#'
#' Does a whole bunch of cool stuff
#'
#' @param price Either a single price or a vector of prices
#' @param in_yr The year in which prices are listed. If vector, then rebase is calculated item-wise. That is, the i-th price is posted in the i-th in_yr USD.
#' @param to_yr The year prices are to be rebased in. By default, rebase converts to 2010 USD.
#' @return A vector of prices
#'
#' @example
#' prices <- runif(10, 50, 100)
#'yrs <- 2000:2009
#'rebase(price=prices, in_yr = yrs, to_yr = 2012)
#'
#' @export
rebase <- function(price, in_yr, to_yr = 2010) {
  stopifnot(length(in_yr) > 0 & length(price) > 0)

  in_cpi <- vapply(in_yr, function(y) cpiData$cpi[cpiData$cpi_year == y], numeric(1))
  to_cpi <- vapply(to_yr, function(b) cpiData$cpi[cpiData$cpi_year == b], numeric(1))

  return(price * to_cpi / in_cpi)
}
bdempe18/toolR documentation built on June 14, 2019, 12:03 a.m.