Try getFX
in package quantmod
. But you must coerce your date column to an object of class "Date"
.
library(quantmod)
exchange <- function(Date, amount, from, to){
data.env <- new.env()
getFX(
Currencies = paste(from, to, sep = "/"),
from = Date,
to = Date,
env = data.env
)
amount * data.env[[paste0(from, to)]]
}
currencies$Date <- as.Date(currencies$Date, "%d/%m/%Y")
GBP <- mapply(
exchange,
currencies$Date,
currencies$USD,
MoreArgs = list(from = "USD", to = "GBP")
)
GBP
#[1] 713.71 367.95
currencies$GBP <- GBP
Data
currencies <- read.table(text = "
Date USD
20/02/2021 1000
16/01/2021 500
", header = TRUE)