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.