Checking if Date is Between two Dates in R

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

    You may use foverlaps from data.table. Convert both the 'data.frame's to 'data.table' with 'start/end' columns. Set the key column as the column names of each dataset. Use foverlaps to get the numeric index which can be converted to binary 'match' based on the NA values in it.

    library(data.table)#v1.9.5+
    dt1 <- data.table(ID=df1$ID, start=df1$actual.date, end=df1$actual.date)
    setkeyv(dt1, colnames(dt1))
    dt2 <- as.data.table(df2)
    setnames(dt2, 2:3, c('start', 'end'))
    setkeyv(dt2, colnames(dt2))
    indx <- foverlaps(dt1, dt2, type='within', which=TRUE, mult='first')
    dt1[, match:= +(!is.na(indx))][,end:=NULL]
    setnames(dt1, 1:2, colnames(df1))
    dt1
    #   ID actual.date match
    #1:  1  1997-10-01     0
    #2:  1  1998-02-01     1
    #3:  1  2002-05-01     1
    #4:  2  1999-07-01     0
    #5:  3  2005-09-01     1
    #6:  4  2003-02-03     1
    #7:  4  2006-05-01     0
    

提交回复
热议问题