Returned item from Vector A or B based on the value of Vector C or D

I'm trying to solve the following problem in R. I have a data frame with 4 columns: currency_1_amt

, currency_1_denom

, currency_2_amt

, currency_2_denom

. Here are some lines from this table

|----------------|------------------|----------------|------------------|
| currency_1_amt | currency_1_denom | currency_2_amt | currency_2_denom |
|----------------|------------------|----------------|------------------|
| 100            | USD              | 620            | CNY              |
| 500            | CNY              | 80.55          | USD              |
| 80.55          | INR              | 1              | USD              |
| 100            | INR              | 9.67           | CNY              |

      

I am trying to compute a conditional value for each line:

  • If currency_1_denom == "USD"

    , then notional value=currency_1_amt

    .

  • If currency_2_denom == "USD"

    , then notional value=currency_2_amt

    .

  • If neither currency_1_denom == "USD"

    , nor currency_1_denom == "USD"

    , then notional_value = currency_1_amt * exchange_rate_of_currency_1_to_USD

    (I have another column in the dataframe with the corresponding exchange rate.

I'm not sure how to do this in R without looping every line. Heer is some pseudo R code that I came up with for this.

result = numeric(length(df))
for(j in 1:length(df)) {
    if(df[j,"currency_1_denom"] == "USD")
        result[j] = currency_1_amt
   else if(df[j,"currency_2_denom"] == "USD")
       result[j] = currency_2_amt
   else
       result[j] = currency_1_amt * lookup_exchange_rate(currency_1_denom)

      

Is there a better (e.g. vectorized) way to accomplish my task?

+3


source to share


2 answers


Re-creating your data:

df<- read.table(text ="currency_1_amt currency_1_denom currency_2_amt  currency_2_denom
100            USD              620             CNY            
500            CNY              80.55           USD            
80.55          INR              1               USD            
100            INR              9.67            CNY",
           header = TRUE, stringsAsFactors = FALSE)

      

Generating a sample exchange rate just to run the example:



df$exchange_rate_of_currency_1_to_USD <- 1:4

      

Usage ifelse

:

df$notional_value <- with(df, ifelse(currency_1_denom == "USD", currency_1_amt, 
                                     ifelse(currency_2_denom == "USD", currency_2_amt,
                                            currency_1_amt * exchange_rate_of_currency_1_to_USD)))

      

+3


source


I would do it just like this:

second <- df$currency_2_denom == "USD"
df$national_value[second] <- df$currency_2_amt[second]
first <- df$currency_1_denom == "USD"
df$national_value[first] <- df$currency_1_amt[first]
third <- (df$currency_1_denom != "USD") & (df$currency_2_denom != "USD")
df$national_value[third] <- df$currency_1_amt[third]*lookup_exchange_rate(currency_1_denom[third])

      



I am reordering as intended (second condition before first), so if the second condition is met, records that also meet the first condition will not be overwritten.

+1


source







All Articles