Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return element from vector A or B based on value of Vector C or D

I am 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 a few rows 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 notional value for each row:

  • 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 data frame with the relevant exchange rate.

I am not sure how to do this in R without looping through each row. Heer is some pseudo R code that I thought up for doing 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 of accomplishing my task?

like image 618
user4970373 Avatar asked Jan 02 '26 07:01

user4970373


2 Answers

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

Creating a sample exchange rate just to run the example:

df$exchange_rate_of_currency_1_to_USD <- 1:4

Using 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)))
like image 178
Carlos Cinelli Avatar answered Jan 04 '26 04:01

Carlos Cinelli


I would do it simply this way:

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 change the order on purpose (second condition before first), so if the second condition is met, the entries fulfilling also the first condition would not be overwrite.

like image 25
Marta Cz-C Avatar answered Jan 04 '26 03:01

Marta Cz-C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!