Convert values to single currency across multiple columns - R and priceR

Solution 1:

Here's a way to do this with getQuote function from quantmod library.

library(dplyr)
library(quantmod)

convert_currency_1 <- function(x) {
  currency <- sub('.*\\s([A-Z]+)', '\\1', x)
  symbol <- paste0(currency, "USD", "=X")
  exchange_rate <- getQuote(symbol)[symbol, ]$Last
  as.numeric(gsub('\\s|[A-Z]+', '', x)) * exchange_rate
}

df_No_2 %>%
    mutate(across(starts_with("Value_"), convert_currency_1))

#  country    Value_1   Value_2  Value_3
#1 Romania   115441.4  23088.29 4617.658
#2      UK 56013986.8 109295.58 6830.974
#3      UK 43718233.6 109295.58 6830.974
#4      UK 43718233.6 109295.58 6830.974