Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Look up a value in different df and take max date in r

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

Spreads_max <- Spreads[which(Spreads$Date == max(Spreads$Date)),]
DF_1 <- Spreads_max[,c(2:4)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading