I have dataset where I need to adjust multiple variables for inflation. It looks something like this.
| year | price1 | price2 | price3 | price4 |
|---|---|---|---|---|
| 2003 | 1.149 | 1.149 | 1.163 | 1.172 |
| 2004 | 1.169 | 1.164 | 1.184 | 1.18 |
| 2005 | 1.167 | 1.166 | 1.183 | 1.178 |
I need to put these all in a constant format (like 2020 dollars). I can do this pretty easily with the adjust_for_inflation function from the priceR package. However, there are a lot of price variables, so I'd like to create them all automatically. I've been trying to do with across but it isn't working. Here's what I've been trying.
library(tidyverse)
library(priceR)
#this is it done manually, which would take hours
df %>% mutate(adjusted_price1=adjust_for_inflation(price1,year,"US",to_date = 2020))
#here's my attempt to do it all at once
price.vars <- df %>% select(-year) %>% names()
dollars2020 <- function(x){
y <- adjust_for_inflation(x,year,"US",to_date = 2020)
}
df <- df %>%
mutate(across(price.vars, dollars2020,.names ="adjusted_{col}"))
As far as I can tell, this should be spitting out a list of new variables with names like adjusted_price1 and so forth. But it's not working. I'd really appreciate any help anyone could give.