Home > Software design >  How to only get one variable when applying a function
How to only get one variable when applying a function

Time:06-16

I have a data table that I want to apply a function over namely the historical_exchange rates function from priceR. I have figured out how to apply this function across my data table however the function spits out two variables. I only want to get the specific exchange rate from the function. So that in the column Par_value_EUR I get the exchange rate on the issuance date. For the first row, this would be 0.108031

My data table looks as follows:

         ISIN    TR.FiCurrency TR.FiIssueDate Par_value_EUR
1: XS1231261907           SEK     2015-05-12            NA
2: XS1231286995           EUR     2015-05-12            NA
3: XS1231416287           HKD     2015-05-19            NA
4: XS1232143310           EUR     2015-05-13            NA
5: XS1232309226           HKD     2015-05-20            NA
6: XS1232498011           USD     2015-05-27            NA

structure(list(ISIN = c("XS1231261907", "XS1231286995", "XS1231416287", 
"XS1232143310", "XS1232309226", "XS1232498011"), TR.FiCurrency = c("SEK", 
"EUR", "HKD", "EUR", "HKD", "USD"), TR.FiIssueDate = structure(c(16567, 
16567, 16574, 16568, 16575, 16582), class = "Date"), Par_value_EUR = c(NA, 
NA, NA, NA, NA, NA)), row.names = c(NA, -6L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x7fc85800cee0>)

I'm applying this function across rows in my data table using apply:

par_values_test$Par_value_EUR <- apply(par_values_test[,c('TR.FiCurrency','TR.FiIssueDate')], 1, 
                                       function(y) historical_exchange_rates(y['TR.FiCurrency'], "EUR", y['TR.FiIssueDate'],y['TR.FiIssueDate'] ) )

The result is as follows:

           ISIN TR.FiCurrency TR.FiIssueDate     Par_value_EUR
1: XS1231261907           SEK     2015-05-12 <data.frame[1x2]>
2: XS1231286995           EUR     2015-05-12 <data.frame[1x2]>
3: XS1231416287           HKD     2015-05-19 <data.frame[1x2]>
4: XS1232143310           EUR     2015-05-13 <data.frame[1x2]>
5: XS1232309226           HKD     2015-05-20 <data.frame[1x2]>
6: XS1232498011           USD     2015-05-27 <data.frame[1x2]>

As you can see this puts a new data frame in the column Par_value_EUR and I only want the second element from this data frame.

CodePudding user response:

We could extract the column with [[. In addition, it may be better to use lapply/Map instead of apply as apply converts to matrix and matrix can have only a single type

library(data.table)
library(priceR)
par_values_test[, Par_value_EUR := 
     unlist(Map(function(x, y) 
       tryCatch(historical_exchange_rates(x, "EUR", y, y),
     error = function(e) data.frame(col1 = NA_real_, col2 = NA_real_))[[2]],
        TR.FiCurrency, TR.FiIssueDate))]

-output

> par_values_test
           ISIN TR.FiCurrency TR.FiIssueDate Par_value_EUR
         <char>        <char>         <Date>         <num>
1: XS1231261907           SEK     2015-05-12      0.108031
2: XS1231286995           EUR     2015-05-12      1.000000
3: XS1231416287           HKD     2015-05-19      0.113934
4: XS1232143310           EUR     2015-05-13      1.000000
5: XS1232309226           HKD     2015-05-20      0.115770
6: XS1232498011           USD     2015-05-27      0.918358

CodePudding user response:

If you only want the second value from the historical_exchange_rates function, you can add a bracket to indicate the indices that you would like returned. See the "[2]" at the very end of the command.

par_values_test$Par_value_EUR <- apply(par_values_test[,c('TR.FiCurrency','TR.FiIssueDate')], 1, 
                                       function(y) historical_exchange_rates(y['TR.FiCurrency'], "EUR", y['TR.FiIssueDate'],y['TR.FiIssueDate'] )[2] )
  • Related