Checking if Date is Between two Dates in R

后端 未结 3 2074
慢半拍i
慢半拍i 2020-11-30 14:54

I have two large datasets, df1 and df2. The first dataset, df1, contains the columns \'ID\' and \'actual.data\'.

df1 <- data.frame(ID=c(1,1,1,2,3,4,4), a         


        
3条回答
  •  心在旅途
    2020-11-30 15:47

    Here is a solution with dplyr

    library(dplyr)
    dat <- inner_join(df1, df2, by = "ID")
    dat %>% rowwise() %>%
            mutate(match = ifelse(between(actual.date, before.date, after.date), 1, 0)) %>%
            select(-c(before.date, after.date)) %>%
            arrange(actual.date, desc(match)) %>%
            distinct(actual.date)
    

    the output is slightly different because it order the actual.date, maybe this is a problem, I'll delete my solution if the case.

    Source: local data frame [7 x 3]
    
      ID actual.date match
    1  1  1997-10-01     0
    2  1  1998-02-01     1
    3  2  1999-07-01     0
    4  1  2002-05-01     1
    5  4  2003-02-03     1
    6  3  2005-09-01     1
    7  4  2006-05-01     0
    

提交回复
热议问题