Merge with Dates

Hi there, I am trying to improve my actual script to make it more functional.
Let me please paste below the result of merging the variables "i" and "u", getting result variable "r".
The condition I need to follow is the below:

merge i$id following the conditions:

         #####  if i$id = u$end.date AND there are no any u$id where  i$date >= u$registration.date AND i$date <u$end.date    ,   then merge i$id = u$end.date   ...... If not, merge i$id when i$date >= u$registration.date AND i$date <u$end.date 
i<-data.frame(id = c(1, 2), date= as.Date.character( c("2019-01-01", "2018-01-01")))



u<-data.frame(id = c(1, 1, 2, 1, 2, 1, 1, 2, 1,2), registration.date= as.Date.character( c("2011-01-01", "2012-01-01",
                                                                                           "2013-01-01", "2014-01-01",
                                                                                           "2015-01-01", "2016-01-01",
                                                                                           "2017-01-01", "2018-02-02",
                                                                                           "2019-01-01", "2020-01-01")),
              end.date= as.Date.character( c("2014-01-01", "2013-01-01",
                                             "2016-01-01", "2017-01-01",
                                             "2018-01-01", "2019-01-01",
                                             "2020-01-01", "2021-01-01",
                                             "2022-01-01", "2023-01-01")),
              Tarif= c(100, 150, 175, 179, 199, 194, 193, 188, 185, 166) )



r <- data.frame(id= c(1,2), date = as.Date.character( c("2019-01-01", "2018-01-01")), 
                registration.date= as.Date.character( c("2019-01-01", "2015-01-01")),
                end.date= as.Date.character( c("2022-01-01", "2018-01-01")), 
                Tarif= c(185, 188))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.