I have a DF, where I want to look up a value in another DF, but only taking the maximum Date of that observation.
DF_1 = as.data.frame(matrix(ncol = 3, nrow = 1))
colnames(DF_1) = c("Financials AAA", "Financials AA", "Financials BBB")
Date = as.Date(c("2022-07-29", "2021-01-01", "2020-01-01"))
Spreads_AAA = c(0.3, 0.5, 0.6)
Spreads_AA = c(0.1,0.2,0.3)
Spreads_BBB = c(1,2,3)
Spreads = data.frame(Date,Spreads_AAA,Spreads_AA, Spreads_BBB)
So at the end I should get 0.3 for AAA, 0.1 for AA and 1 for BBB in the DF1 in the only row I have
Thank you
>Solution :
Try this:
Spreads_max <- Spreads[which(Spreads$Date == max(Spreads$Date)),]
DF_1 <- Spreads_max[,c(2:4)]