I used the getFX function from the Quantmod package in R to generate a vector of rates from Oanda, each in xts zoo format.
currency_pairs <- c("GBP/USD", "USD/SGD")
rates <- getFX(currency_pairs, from="2019/01/01", to="2019/01/01"
This returns a vector of xts zoo objects in the form:
(GBPUSD, USDSGD,...)
However I would like to have just the rates, since I only require the rates for one date and therefore know the timestamp.
I have tried looping over the vector like so:
for (i in 1:length(rates){
rates[i] <- coredata(rates[i])
}
but this just returns the currency pair name.
What you could do in this case, if you only retrieve data for one date, is use to sapply
like this:
library(quantmod)
currency_pairs <- c("GBP/USD", "USD/SGD")
# for 1 date this will return a named vector otherwise use lapply
rates <- sapply(currency_pairs, getFX, from="2019/01/01", to="2019/01/01", auto.assign = FALSE)
rates
GBP/USD USD/SGD
1.275455 1.362920
Normally I would suggest using lapply to retrieve all the currencies in a big list and then access the list with lapply / mapply / Map / purrr::map etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.