Checking if Date is Between two Dates in R

后端 未结 3 2043
慢半拍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:49

    Just another hopefully correct answer using the fuzzyjoin package.

    library(data.table)
    library(fuzzyjoin)
    dt1 <- data.table(df1)
    dt2 <- data.table(df2)
    
    fuzzy_left_join(dt1
                    , dt2, 
                    by = c("ID" = "ID", "actual.date" = "before.date", "actual.date" = "after.date"), 
                    match_fun = list(`==`, `>`, `<`))[,.(ID = ID.x
                                                         ,actual.date
                                                         , match = ifelse(is.na(ID.y),0,1))]
    

提交回复
热议问题