fnas
fnas

Reputation: 55

Converting Currencies in R

I am working on a project in R that requires me to change a bunch of different currencies to USD. I started by separating the numeric value from the associated currency and placing them in two new columns by using the following code:

```
#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]+")
```

I am a little confused now on how I can convert the different currencies to USD. I tried creating a function with a if statement and then run the function in sapply() but it crashed my R:

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

**The 0.014 is the INR-USD rate

The supply() call is as follows:

z <- sapply(dataframe$BudgetCurrency, convertCurrency)

A few lines of my data are:

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

I would appreciate any suggestions/help that anyone could offer! If you need any additional information, please let me know!

Upvotes: 3

Views: 647

Answers (2)

David J. Bosak
David J. Bosak

Reputation: 1624

Here is another way to do it using a 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 package.

Obviously, I'm making up the conversions. But you can get the idea:

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

Upvotes: 1

Adam Sampson
Adam Sampson

Reputation: 2021

The question isn't repeatable enough from the code snippit for me to be sure what you wanted, so I've made some guesses. In the future you might try to post sample data and what the desired output would be from that sample data.

The code below shows you one way (using base R) to do an if statement to return a value. There is another way to do this using dplyr that is more readable, but you have only used base R so far.

# 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)

Upvotes: 0

Related Questions