Unfortunately, adjusting daily USD prices for inflation in R is not as straightforward as one might hope. It requires the download of the monthly Consumer Price Index statistics (CPI) from the US Bureau of Labor Statistics (BLS) using the blscrapeR library. And then some fiddling with the dates before the inflation factors can be merged into the price table.

Downloading the CPI statistics from BLS

We start by downloading the CPI statistics from BLS. Make sure the base_date lies before the first entry in your USD price table. Although this base_date forms the base of the inflation table returned — starting with the value 1.00 — it doesn't matter if you pick a date before the first date of your price table: if we want we can simply rebase by dividing all price values by the inflation factor at the start of the price table.

# libraries
library(blscrapeR)  # for BLS/inflation functions
# get inflation stats
if (!exists ("BLSinflation")) { # only once every session
BLSinflation <- inflation_adjust(base_date="2008-01-01") %>% arrange(desc(date))
}

As you can see, we download these statistics only once per session — this because access to these stats is limited without an API key.

Note that the BLSinflation table is a tibble, i.e. an extended/lazy dataframe.

Removing double entries

Also be aware that the table returned by the inflation_adjust call for some reason contained double entries for the years 2023. So we removed these duplicates like this:

BLSinflation <- distinct(BLSinflation, date, .keep_all=TRUE)

Merging inflation factors

Now we can use the BLSinflation table to add a column of inflation factors our USDprices table.

Since the BLSinflation table contains only entries for the first day of each month, and our USDprices table has a column with prices for each day, we will have to set the inflation factor for each day to the value for the first day of the month. This is accomplished by creating a new auxiliary date column YearMonthFirst that holds the year and the month for the current row, but always for the first day of the month. Now we can use this YearMonthFirst column to merge the inflation factors from the BLSinflation table.

#  merge inflation factors
USDprices$YearMonthFirst <- as.Date(format(USDprices$time, "%Y-%m-01"))
USDprices <- merge (USDprices, BLSinflation[c("date", "adj_dollar_value")],
    by.x="YearMonthFirst", by.y="date", all.x=TRUE, all.y=FALSE)
# sort by date
BTCdata <- BTCdata[order(BTCdata$time),]

Adjusting the USD prices

In this final step from the existing column PriceUSD we create a new column adjustedPriceUSD that holds the prices adjusted to current dollars. We do so by first dividing all original prices by their associated inflation factors, and then multiplying with the current (last) inflation factor (in USDprices_last_inflation_adjustment).

# adjust USD prices (new column: "adjustedPriceUSD")
USDprices <- USDprices[order(USDprices$time),]  # sort by date
USDprices <- fill(USDprices, "adj_dollar_value", .direction="down")  # fill NA values of inflation with last value
# ajdPrice = orgPrice / orgFactor * currentFactor
USDprices_last_inflation_adjustment <- last(USDprices$adj_dollar_value)
USDprices$adjustedPriceUSD <- USDprices$PriceUSD / USDprices$adj_dollar_value * USDprices_last_inflation_adjustment

And we're all set! The new column adjustedPriceUSD now contains the daily prices adjusted to current dollars.

Add comment

Security code Refresh

Submit