Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This post shows how to read daily historical exchange rates given symbols as a string.

I collected the symbols of exchnage rates at

https://finance.yahoo.com/currencies

### R code

The following R code retrieves historical daily exchange rates given their symbols as of 2022-08-14.

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 #========================================================## Quantitative ALM, Financial Econometrics & Derivatives # ML/DL using R, Python, Tensorflow by Sang-Heon Lee ## https://kiandlee.blogspot.com#——————————————————–## read historical exchange rates#========================================================# graphics.off(); rm(list = ls()) library(quantmod)library(stringr) # trim #————————————————-# Symbols of exchange rates, as of 2022-08-14#————————————————-vstr_symbol <– “    Symbol  ,    Name    EURUSD=X,    EUR/USD       JPY=X   ,    USD/JPY      GBPUSD=X,    GBP/USD    AUDUSD=X,    AUD/USD    NZDUSD=X,    NZD/USD    EURJPY=X,    EUR/JPY    GBPJPY=X,    GBP/JPY    EURGBP=X,    EUR/GBP    EURCAD=X,    EUR/CAD    EURSEK=X,    EUR/SEK    EURCHF=X,    EUR/CHF    EURHUF=X,    EUR/HUF    CNY=X   ,    USD/CNY    HKD=X   ,    USD/HKD    SGD=X   ,    USD/SGD    INR=X   ,    USD/INR    MXN=X   ,    USD/MXN    PHP=X   ,    USD/PHP    IDR=X   ,    USD/IDR    THB=X   ,    USD/THB    MYR=X   ,    USD/MYR    ZAR=X   ,    USD/ZAR    RUB=X   ,    USD/RUB    “ #——————————————-# split symbols and make vector#——————————————-df <– read.table(text = str_trim(vstr_symbol),                  sep = “,”, header = TRUE)df <– as.data.frame(df); df df$Symbol <– str_trim(gsub(“[\t\r\n,]”, “”, df$Symbol))df$Name <– str_trim(gsub(“[\t\r\n,]”, “”, df$Name))dfnc <– nrow(df) # number of exchange rate #——————————————-# read price information#——————————————-sdate <– as.Date(“2004-01-01”)edate <– as.Date(“2022-07-31”)getSymbols(df$Symbol, from=sdate, to=edate) #——————————————-# collect only adjusted prices#——————————————-price <– NULLfor(i in 1:nc) { eval(parse(text=paste0( “price <- cbind(price,", gsub(“\\^”,“”,df$Symbol[i]),“[,6])”)))} # modify column Name as only symbolcolnames(price) <– gsub(“.X.Adjusted”, “”, colnames(price)) # convert to data.frame with the first column as Datedf.price <– cbind(time=time(price), as.data.frame(price))rownames(df.price) <– NULL #——————————————-# print time series of daily prices#——————————————-head(df.price,3)tail(df.price,3) Colored by Color Scripter cs

Running the above R code displays the status of data reading process as follows.

Finally, we can get the collection of individual exchange rates.