我有以下数据框:
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")
我想添加一个新列,其中“价格”的值为欧元,采用 2019 年的平均汇率。
我已经安装了 priceR 包并测试了以下功能,但它不允许从多种货币转换。
historical_exchange_rates("NZD", to = "USD",
start_date = "2019-01-01", end_date = "2019-12-31")
以 2019 年的平均欧元价格添加新列的优雅方法是什么?
输出应如下所示:
currency price price_euro
NZD 380.86 500
SEK 531.75 800
######## 编辑 #####
我设法用货币创建了这个 df,然后我将 left_join 到我的主 df。我想知道是否有更优雅的解决方案。
#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))
货币数据框(在 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")
具有平均和重塑的currency_df
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"))