I have two df:
df1 <- data.frame(DTS = c(as.Date("2009-12-12"), as.Date("2012-12-12") , as.Date("2015-3-4"),
as.Date("2018-7-9")),score= c(10,7,3,8))
df2 <- data.frame(DTS = c(as.Date("2009-12-14"), as.Date("2013-09-12") ,as.Date("2014-09-12"),
as.Date("2015-3-4"),as.Date("2016-05-05"), as.Date("2019-12-12")))
I need to find the score for the relevant dates in df2 by looking up in df1
This works, but it is far from elegant.. anyone with a better solution?
df2$score <- 0
for (int_x in 1:NROW(df1)) {
df2$score[ df2$DTS <= df1$DTS[int_x+1] & df2$DTS> df1$DTS [int_x] ] <- df1$score[int_x]
}
df2$score[df2$DTS > df1$DTS[nrow(df1)]] <- df1$score[NROW(df1)]