【问题标题】:Converting Currencies in R在 R 中转换货币
【发布时间】:2021-02-24 19:17:13
【问题描述】:

我正在使用 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 是印度卢比兑美元汇率

supply() 调用如下:

z <- sapply(dataframe$BudgetCurrency, convertCurrency)

我的几行数据是:

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

如果有人可以提供任何建议/帮助,我将不胜感激!如果您需要任何其他信息,请告诉我!

【问题讨论】:

  • 到目前为止你做得很好。您可以编辑您的帖子以包含几行数据,并添加您的sapply() 电话吗?谢谢。
  • @DavidJ.Bosak 感谢您的回复!我对 StackOverflow 很陌生,我不知道如何向 cmets 添加代码,所以我用您要求的信息编辑了原始帖子!请让我知道是否有方法可以改进代码或者我是否犯了任何错误。
  • 两件事。首先,在您的函数中,您将传递一个变量function(currencyVal) 以在函数内部使用。但是你永远不会在函数内部使用它,而是从全局环境中调用整个数据框。在您的函数中,您可以将向量 dataframe$BudgetCurrency 替换为单个值 currencyVal
  • 第二,这里不需要申请。您可以使用 ifelse() 而不是 if 以矢量形式执行此操作。
  • 最后,只有当!is.na(dataframe$BudgetCurrency) 为真时,!is.na(dataframe$BudgetCurrency == 'INR') 才会为真。 == 返回 TRUE 或 FALSE,除非一侧为 NA。

标签: r sapply


【解决方案1】:

这是使用datastep 的另一种方法。基本上,这是在逐行遍历数据框,并让您对变量的值做出决定。它是libr 包的一部分。

显然,我正在弥补转化。但你可以明白:

library(libr)

# Create sample data
dataframe <- read.table(header = TRUE, text = '
imdb_title  budget     BudgetNum   BudgetCurrency
tt000009   $2500       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       $2500      2500           <NA>            2500
# 2  tt0000011    ITL60000    600000            ITL          900000
# 3  tt0000012   ROL950000   9500000            ROL         7885000
# 4  tt0000087 INR52000000  52000000            INR          728000

【讨论】:

  • 非常感谢您。软件包库是惊人的!我决定将所有货币添加到列表中,然后将其转换为向量,以便计算更高效。
【解决方案2】:

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

下面的代码向您展示了一种方法(使用基数 R)执行 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)

【讨论】:

  • 感谢您的帮助!我不敢相信我忽略了我的 else 声明。感谢您引起我的注意
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多