1

我有以下数据框:

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"))
4

2 回答 2

0

如果我理解您的问题,您只需在现有数据集中添加一列,其中包含欧元价格。我认为您对加入数据集有正确的想法,但是由于您提供的函数格式化答案的方式,因此需要处理一些丑陋的事情。我还想像您想要一种将其推广到更大的类似结构化数据集的方法,因此将其功能化是可取的。

我确信有一种更有效的方法,但对于像我这样的爱整洁者来说,这很有效。

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)

生成以下输出

  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
于 2020-12-18T00:08:07.477 回答
0

我认为您已经有了一个解决方案,但如果它有用,这里有一种方便的方法来生成具有多种货币的数据框:

library(priceR)
library(tidyverse)

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

nz_to_eu <- historical_exchange_rates("NZD", to = "EUR",
                          start_date = "2019-01-01", end_date = "2019-12-31")

nz_to_us %>% 
  left_join(nz_to_eu, by = "date")

          date one_NZD_equivalent_to_x_USD one_NZD_equivalent_to_x_EUR
1   2019-01-01                    0.672695                    0.585309
2   2019-01-02                    0.671352                    0.585720
3   2019-01-03                    0.663423                    0.586166
4   2019-01-04                    0.668918                    0.587234
5   2019-01-05                    0.668918                    0.587234
6   2019-01-06                    0.668918                    0.587234
7   2019-01-07                    0.674390                    0.591051
8   2019-01-08                    0.675413                    0.588339
9   2019-01-09                    0.674081                    0.588408
10  2019-01-10                    0.679107                    0.587717

于 2020-12-21T11:31:44.417 回答