I have two data frames - the first with dates and currencies, the second with dates, currencies and exchange rates. Here's a reproducible shortened example:
df1 <- data.frame(date = c(1:5),
currency = c("USD", "SEK", "DKK", "USD", "EUR"),
rate = rep(NA, 5))
df2 <- data.frame(date = rep(c(1:5), each = 4),
currency = rep(c("EUR", "DKK", "SEK", "USD")),
rate = abs(rnorm(20)))
I would like to transfer the correct rate from data frame 2 to data frame 1 matched on currency and date.
I prefer not to use loops if possible, so I tried this:
df1$rate <- df2$rate[df2$date == df1$date & df2$currency == df1$currency]
which transferred a single rate to all of the NAs in df1.
Apologies if I haven't explained myself well, I'm new to R.
Thanks for any help.