[This article was first published on R Tricks – Data Science Riot!, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I was surprised to see there weren’t more of these types of calculators in the R community. Inflation and adjusted payments seem like they would be more common. I was able to find a way to gather Consumer Price Index data using the quantmod package but quantmod leaves you to your own devices in converting the data. So, I whipped up a formula, this will most likely be incorporated into my upcoming BLS scrape package.

The function grabs the CPI data as a flat file, converts the data to an xts object and finds the average CPI index for each year. It then calculates an adjusted “dollar” by dividing the average CPI for your target year to your base year and uses that multiplier to calculate the value of a dollar for any given year related to your base year.

Note: for this to work you must have a “base year,” i.e. something to compare to. So, if I wanted to know the current-day value of $1.00 USD from 1947, I would use 1947 for my base year and find out the 2015 value is$10.62.

The Results:

 date base.year adj_value pct_increase 12/1/10 1947 9.78 878.0269058 12/1/11 1947 10.09 908.5201794 12/1/12 1947 10.3 929.5964126 12/1/13 1947 10.45 944.8430493 12/1/14 1947 10.61 961.4349776 10/1/15 1947 10.62 961.8834081

To check the math, I can go to the BLS inflation calculator web-app.

The Code:

inflation_adjust <- function(base_year=NA){
require(dplyr)
require(xts)
if (nchar(base_year) == 4){
temp<-tempfile()
sep="t",
skip=1,
stringsAsFactors=FALSE,
strip.white=TRUE)
colnames(cu_main)<-c("series_id", "year", "period", "value", "footnote_codes")
#Get rid of annual time periods and add real dates.
cu_main <- subset(cu_main,series_id=="CUSR0000SA0")
cu_main <- subset (cu_main,period!="M13")
cu_main <- subset (cu_main,period!="S01")
cu_main <- subset (cu_main,period!="S02")
cu_main <- subset (cu_main,period!="S03")
cu_main$date <-as.Date(paste(cu_main$year, cu_main$period,"01",sep="-"),"%Y-M%m-%d") cu_main <- cu_main[c('date','value')] cu_main <- xts(cu_main[,-1], order.by=cu_main[,1]) cu_main <- round(cu_main, 1) # Get average yearly CPI. avg.cpi <- apply.yearly(cu_main, mean) avg.cpi <- round(avg.cpi, 1) # Formula for calculating inflation example:$1.00 * (1980 CPI/ 2014 CPI) = 1980 price
cf <- avg.cpi/as.numeric(avg.cpi[as.character(base_year)])
#cf <- round(cf, 2)
dat <- merge(avg.cpi, cf)
# Xts object to data frame
dat <- data.frame(date=index(dat), coredata(dat))
dat$base.year <- as.character(base_year) dat$pct_increase <- (1-dat$adj_value) * -100 # Round dollar amounts dat$adj_value <- round(dat\$adj_value, 2)
# Reorder cols in a more usable fassion
return(dat)
}
else {(message(
"***************************************************************************************
Please input a valid four digit year without quotes. For example: 2015.
***************************************************************************************", appendLF = TRUE))
}
}



Photo by che1899