在 R 中转换货币

Converting Currencies in R

我正在用 R 开发一个项目,该项目要求我将一堆不同的货币换成美元。我首先将数值与相关货币分开,然后使用以下代码将它们放在两个新列中:

```
#Extract numeric values from budget
dataframe$BudgetNum <- as.numeric(str_extract(dataframe$budget, "[0-9]+"))

#Extract character (currency) values from budget
dataframe$BudgetCurrency <- str_extract(dataframe$budget, "[aA-zZ]+")
```

我现在对如何将不同的货币兑换成美元感到有点困惑。我尝试用 if 语句创建一个函数,然后 运行 sapply() 中的函数,但它使我的 R:

崩溃了
convertCurrency <- function(currencyVal){
  if(!is.na(dataframe$BudgetCurrency == 'INR')){
    BudgetNum = merge.movies.filter$BudgetNum*0.014
    return(BudgetNum)
    }
}

**0.014 是 INR-USD 汇率

supply()调用如下:

z <- sapply(dataframe$BudgetCurrency, convertCurrency)

我的几行数据是:

    imdb_title  budget     BudgetNum   BudgetCurrency
    tt000009   00       2500        *NA*
    tt0000011  ITL60000    600000      ITL
    tt0000012  ROL950000   9500000     ROL
    tt0000087  INR52000000 52000000    INR

如果有人能提供任何 suggestions/help,我将不胜感激!如果您需要任何其他信息,请告诉我!

这个问题在代码片段中的可重复性不足以让我确定你想要什么,所以我做了一些猜测。将来您可能会尝试 post 样本数据以及该样本数据所需的输出。

下面的代码向您展示了一种方法(使用基础 R)对 return 值执行 if 语句。还有另一种方法可以使用 dplyr 来做到这一点,它更具可读性,但到目前为止你只使用了 base R。

# Original function from post
# convertCurrency <- function(currencyVal){
#   # If the dataframe$BudgetCurrency is NA
#   # Did the poster mean to post If the dataframe$BudgetCurrency is not 'INR'?
#   if(!is.na(dataframe$BudgetCurrency == 'INR')){
#     # get the value 
#     BudgetNum = merge.movies.filter$BudgetNum*0.014
#     return(BudgetNum)
#   }
#   # Otherwise...return the value of the if statement.
#   # This is probably an oversight where the poster didnt' know that something
#   # is always returned and they left it open ended.
# }

z <- ifelse(dataframe$BudgetCurrency != 'INR',
            merge.movies.filter$BudgetNum*0.014,
            merge.movies.filter$BudgetNum)

这是使用 datastep. Basically what this is doing is looping through the data frame row by row, and letting you make decisions on the values of the variables. It is part of the libr 包的另一种方法。

显然,我正在弥补转化。但是你可以得到这个想法:

library(libr)

# Create sample data
dataframe <- read.table(header = TRUE, text = '
imdb_title  budget     BudgetNum   BudgetCurrency
tt000009   00       2500        NA
tt0000011  ITL60000    600000      ITL
tt0000012  ROL950000   9500000     ROL
tt0000087  INR52000000 52000000    INR')

# Run datastep
dataframe2 <- datastep(dataframe, {
                       
         if (is.na(BudgetCurrency))
           ConvertedAmount <- BudgetNum
         else if (BudgetCurrency == "ITL")
           ConvertedAmount <- BudgetNum * 1.5
         else if (BudgetCurrency == "ROL")
           ConvertedAmount <- BudgetNum * .83
         else if (BudgetCurrency == "INR")
           ConvertedAmount <- BudgetNum * 0.014
         
       })

# View results
dataframe2
#   imdb_title      budget BudgetNum BudgetCurrency ConvertedAmount
# 1   tt000009       00      2500           <NA>            2500
# 2  tt0000011    ITL60000    600000            ITL          900000
# 3  tt0000012   ROL950000   9500000            ROL         7885000
# 4  tt0000087 INR52000000  52000000            INR          728000