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"))
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.
Which generates the following output