rcurrency-exchange-rates

Convert into EURO from multiple currencies in R


I have the following dataframe:

structure(list(currency = c("NZD", "NZD", "NZD", "NZD", "NZD", 
"EUR", "SEK", "EUR"), price = c(580.9, 539.75, 567.8, 
802, 486, 365, 5088, 111)), class = "data.frame")

I would like to add a new column with the value of "price" in EUR taking the average exchange rate of 2019.

I have installed the priceR package and tested the following function, but it does not allow to convert from multiple currencies.

historical_exchange_rates("NZD", to = "USD",
                          start_date = "2019-01-01", end_date = "2019-12-31")

What could be an elegant way to add a new column with the average 2019 price in Euro?

The output should look like this:

 currency            price        price_euro
      NZD            380.86       500
      SEK            531.75       800

######## EDIT #####

I managed to create this df with currencies that I will then left_join to my main df. I was wondering if there is a more elegant solution.

#include currencies
currency <- unique(mydf$currency)
#Loop over each of them
currency_df <- do.call(cbind, lapply(currency, function(x) {
    historical_exchange_rates(currency, to = "EUR",
                              start_date = "2019-01-01", end_date = "2019-12-31")}))
#remove duplicated columns (date)
currency_df <- currency_df[, !duplicated(colnames(currency_df), fromLast = TRUE)] 

#clean the currency df
currency_df <- currency_df %>% 
    #gets the average of all numeric columns
    summarise_if(is.numeric, mean, na.rm =TRUE) %>% 
    #reshape from wide to long
    pivot_longer(cols = starts_with("one_")) %>% 
    #extract currency name to link to main table  
     mutate(currency = gsub(".*one_(.*)_equivalent.*","\\1",name))

the currency dataframe (after do.call):

structure(list(date = structure(17906, class = "Date"), one_NZD_equivalent_to_x_EUR = 0.587717, 
    date = structure(17906, class = "Date"), one_KES_equivalent_to_x_EUR = 0.008648, 
    date = structure(17906, class = "Date"), one_USD_equivalent_to_x_EUR = 0.865426, 
    date = structure(17906, class = "Date"), one_AED_equivalent_to_x_EUR = 0.235849, 
    date = structure(17906, class = "Date"), one_EUR_equivalent_to_x_EUR = 1, 
    date = structure(17906, class = "Date"), one_TRY_equivalent_to_x_EUR = 0.158195, 
    date = structure(17906, class = "Date"), one_CZK_equivalent_to_x_EUR = 0.039034, 
    date = structure(17906, class = "Date"), one_PLN_equivalent_to_x_EUR = 0.23245, 
    date = structure(17906, class = "Date"), one_ZAR_equivalent_to_x_EUR = 0.062471, 
    date = structure(17906, class = "Date"), one_GBP_equivalent_to_x_EUR = 1.10791, 
class = "data.frame") 

the currency_df with average and reshape

structure(list(name = c("one_NZD_equivalent_to_x_EUR", "one_KES_equivalent_to_x_EUR", 
"one_USD_equivalent_to_x_EUR", "one_AED_equivalent_to_x_EUR", 
"one_EUR_equivalent_to_x_EUR", "one_CHF_equivalent_to_x_EUR", 
"one_SEK_equivalent_to_x_EUR", "one_NOK_equivalent_to_x_EUR", 
"one_DKK_equivalent_to_x_EUR", "one_TRY_equivalent_to_x_EUR", 
"one_CZK_equivalent_to_x_EUR", "one_PLN_equivalent_to_x_EUR", 
"one_ZAR_equivalent_to_x_EUR", "one_GBP_equivalent_to_x_EUR", 
"one_HKD_equivalent_to_x_EUR", "one_SGD_equivalent_to_x_EUR", 
"one_INR_equivalent_to_x_EUR", "one_AUD_equivalent_to_x_EUR", 
"one_AOA_equivalent_to_x_EUR"), value = c(0.588651219178082, 
0.0930875424657534, 0.89324564109589, 0.307407216438356, 1, 0.89912858630137, 
0.0945236684931507, 0.101572109589041, 0.133948753424658, 0.157569854794521, 
0.0389822712328767, 0.232789550684932, 0.0618727479452055, 1.14057644657534, 
0.11402897260274, 0.654955421917808, 0.0127049808219178, 0.620929498630137, 
1), currency = c("NZD", "KES", "USD", "AED", "EUR", "CHF", "SEK", 
"NOK", "DKK", "TRY", "CZK", "PLN", "ZAR", "GBP", "HKD", "SGD", 
"INR", "AUD", "AOA")), row.names = c(NA, -19L), class = c("tbl_df", 
"tbl", "data.frame"))

Solution

  • If I understand your question, you simply want to add a column to the existing dataset that contains the price in euro. I think you have the right idea with joining datasets, but because of the way that function you provided formats the answer there's just a few ugly things that need tending to. I also imagine you'd like a way of generalizing this to larger similarly structured datasets and so funtion-izing it is preferable.

    I'm sure there's a more efficient way, but for a tidyphile like me this works.

    library(priceR);library(tidyverse)
    
    #Data frame
    df <- data.frame(
      currency = c("NZD", "NZD", "NZD", "NZD", "NZD", "EUR", "SEK", "EUR"),
      price = c(580.9, 539.75, 567.8, 802, 486, 365, 5088, 111)
    )
    
    #Function to pull conversions
    avg_ex <- function(x){
      historical_exchange_rates(x, to = "EUR",start_date = "2019-01-01", end_date = "2019-12-31") %>%
        `colnames<-`(c('date','conv')) %>% summarise(mean(conv)) %>% as.numeric
    }
    
    #Apply across all needed
    conversions = sapply(unique(df$currency),avg_ex) %>% data.frame() %>% rownames_to_column() %>%
      `colnames<-`(c('currency','conv'))
    
    #Join and convert
    df %>% left_join(conversions,by='currency') %>%
      mutate(price_euro = price*conv)
    

    Which generates the following output

      currency   price       conv price_euro
    1      NZD  580.90 0.58865122   341.9475
    2      NZD  539.75 0.58865122   317.7245
    3      NZD  567.80 0.58865122   334.2362
    4      NZD  802.00 0.58865122   472.0983
    5      NZD  486.00 0.58865122   286.0845
    6      EUR  365.00 1.00000000   365.0000
    7      SEK 5088.00 0.09452367   480.9364
    8      EUR  111.00 1.00000000   111.0000