跨多个列将值转换为单一货币 - R 和 priceR
Convert values to single currency across multiple columns - R and priceR
我有一个数据框,其中不同的货币分布在多个列中,我需要将所有值转换为相同的货币 (USD)。
这是一个样本 df:
df <- data.frame(country = c("Romania", "Hungary", "Bulgaria", "Czechia"),
Value_1 = c("500 000.00 RON", "520 000.00 HUF", "480 000.00 BGN", "460 000.00 CZK"),
Value_2 = c("100 000.00 RON", "320 000.00 HUF", "580 000.00 BGN", "660 000.00 CZK"),
Value_3 = c("20 000.00 RON", "10 000.00 HUF", "60 000.00 BGN", "50 000.00 CZK"))
我看过几个 as well as the priceR package。我对如何处理它有点困惑——是否可以编写一个函数来转换数据集中的所有货币,或者我是否需要首先将货币与要转换的金额分开?这是我第一次必须在数据集中转换货币 - 任何帮助将不胜感激。
编辑
刚刚尝试了 Ronak 建议的解决方案,但注意到对于相同的货币和价值有一些不寻常的结果 - 这些肯定会被转换为相同的价值而不是不同的?请参阅下面的新示例数据框和代码:
df_No_2 <- data.frame(country = c("Romania", "UK", "UK", "UK"),
Value_1 = c("500 000.00 RON", "41 000 000.00 GBP", "32 000 000.00 GBP", "32 000 000.00 GBP"),
Value_2 = c("100 000.00 RON", "80 000.00 GBP", "80 000.00 GBP", "80 000.00 GBP"),
Value_3 = c("20 000.00 RON", "5 000.00 GBP", "5 000.00 GBP", "5 000.00 GBP"))
convert_currency_1 <- function(x) {
currency <- sub('.*\s([A-Z]+)', '\1', x)
exchange_rate <- getQuote(paste0(currency, "USD", "=X"))$Last
as.numeric(gsub('\s|[A-Z]+', '', x)) * exchange_rate
}
df_No_2 <- df_No_2 %>%
mutate(across(starts_with("Value_"), convert_currency_1))
这是使用 quantmod
库中的 getQuote
函数执行此操作的方法。
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
我有一个数据框,其中不同的货币分布在多个列中,我需要将所有值转换为相同的货币 (USD)。
这是一个样本 df:
df <- data.frame(country = c("Romania", "Hungary", "Bulgaria", "Czechia"),
Value_1 = c("500 000.00 RON", "520 000.00 HUF", "480 000.00 BGN", "460 000.00 CZK"),
Value_2 = c("100 000.00 RON", "320 000.00 HUF", "580 000.00 BGN", "660 000.00 CZK"),
Value_3 = c("20 000.00 RON", "10 000.00 HUF", "60 000.00 BGN", "50 000.00 CZK"))
我看过几个
编辑
刚刚尝试了 Ronak 建议的解决方案,但注意到对于相同的货币和价值有一些不寻常的结果 - 这些肯定会被转换为相同的价值而不是不同的?请参阅下面的新示例数据框和代码:
df_No_2 <- data.frame(country = c("Romania", "UK", "UK", "UK"),
Value_1 = c("500 000.00 RON", "41 000 000.00 GBP", "32 000 000.00 GBP", "32 000 000.00 GBP"),
Value_2 = c("100 000.00 RON", "80 000.00 GBP", "80 000.00 GBP", "80 000.00 GBP"),
Value_3 = c("20 000.00 RON", "5 000.00 GBP", "5 000.00 GBP", "5 000.00 GBP"))
convert_currency_1 <- function(x) {
currency <- sub('.*\s([A-Z]+)', '\1', x)
exchange_rate <- getQuote(paste0(currency, "USD", "=X"))$Last
as.numeric(gsub('\s|[A-Z]+', '', x)) * exchange_rate
}
df_No_2 <- df_No_2 %>%
mutate(across(starts_with("Value_"), convert_currency_1))
这是使用 quantmod
库中的 getQuote
函数执行此操作的方法。
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