按组查找期间间隔内的日期

时间:2017-05-26 11:24:18

标签: r date datatable overlap lubridate

我有一个包含许多ID,beginend日期的面板。 beginend日期创建interval时间。

    id      begin        end                       interval overlap
 1:  1 2010-01-31 2011-06-30 2009-08-04 UTC--2011-12-27 UTC    TRUE
 2:  1 2011-01-31 2012-06-30 2010-08-04 UTC--2012-12-27 UTC    TRUE
 3:  1 2012-01-31 2013-06-30 2011-08-04 UTC--2013-12-27 UTC    TRUE
 4:  1 2013-01-31 2014-06-30 2012-08-04 UTC--2014-12-27 UTC    TRUE
 5:  1 2013-02-28 2013-07-31 2012-09-01 UTC--2014-01-27 UTC    TRUE
 6:  1 2015-02-28 2015-03-31 2014-09-01 UTC--2015-09-27 UTC    TRUE
 7:  1 2015-06-30 2015-07-31 2015-01-01 UTC--2016-01-27 UTC    TRUE
 8:  1 2015-09-30 2016-01-31 2015-04-03 UTC--2016-07-29 UTC    TRUE
 9:  2 2010-01-31 2011-06-30 2009-08-04 UTC--2011-12-27 UTC    TRUE
10:  2 2011-01-31 2012-06-30 2010-08-04 UTC--2012-12-27 UTC    TRUE
11:  2 2012-01-31 2013-06-30 2011-08-04 UTC--2013-12-27 UTC    TRUE
12:  2 2013-01-31 2014-06-30 2012-08-04 UTC--2014-12-27 UTC    TRUE
13:  2 2013-02-28 2013-07-31 2012-09-01 UTC--2014-01-27 UTC    TRUE
14:  2 2015-02-28 2015-03-31 2014-09-01 UTC--2015-09-27 UTC    TRUE
15:  2 2015-06-30 2015-07-31 2015-01-01 UTC--2016-01-27 UTC    TRUE
16:  2 2015-09-30 2016-01-31 2015-04-03 UTC--2016-07-29 UTC    TRUE

我需要测试,对于每个ID,任何begin / end日期是否包含在另一个interval(具有相同ID)中。

例如,id1 begin(2010-01-31)未包含在id1的任何其他时段中,而不包括在第一行中。但是,id1 end日期(2011-06-30)包含在第二行的间隔中(2010-08-04 UTC - 2012-12-27 UTC)。

我在数据表中尝试了lubridate时间间隔和%within%,但它产生了TRUE,因为它包含在相应的时间段内。我需要知道它是否包含在同一ID的任何其他时间段内。

customer[begin %within% interval | end %within% interval, overlap := TRUE, by = id]

我已经检查了data.table的foverlap,但似乎设计用于连接不同的表,而其他问题只是向量,但not panels有间隔。

有什么想法吗?

数据:

structure(list(id = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 
2, 2, 2), begin = structure(c(14640, 15005, 15370, 15736, 15764, 
16494, 16616, 16708, 14640, 15005, 15370, 15736, 15764, 16494, 
16616, 16708), class = "Date"), end = structure(c(15155, 15521, 
15886, 16251, 15917, 16525, 16647, 16831, 15155, 15521, 15886, 
16251, 15917, 16525, 16647, 16831), class = "Date"), interval = structure(c(75600000, 
75686400, 75686400, 75600000, 44323200, 33782400, 33782400, 41731200, 
75600000, 75686400, 75686400, 75600000, 44323200, 33782400, 33782400, 
41731200), start = structure(c(1249344000, 1280880000, 1312416000, 
1344038400, 1346457600, 1409529600, 1420070400, 1428019200, 1249344000, 
1280880000, 1312416000, 1344038400, 1346457600, 1409529600, 1420070400, 
1428019200), tzone = "UTC", class = c("POSIXct", "POSIXt")), tzone = "UTC", class = structure("Interval", package = "lubridate")), 
    overlap = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
    TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)), .Names = c("id", 
"begin", "end", "interval", "overlap"), row.names = c(NA, -16L
), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000000140788>)

1 个答案:

答案 0 :(得分:2)

使用int_overlaps中的lubridate,这是一种方法。我已经定义了开始日期和结束日期的间隔,尽管在您的数据中它们是不同的 - 也许您可以澄清哪个是正确的。

library(lubridate)

df$interval <- interval(as.POSIXct(df$begin),as.POSIXct(df$end))

df <- df[order(df$id),] #needs to be sorted by id for next stage to work

df$overlap <- unlist(tapply(df$interval, #loop through intervals
                            df$id, #grouped by id
                            function(x) rowSums(outer(x,x,int_overlaps))>1))
                                   #check if more than one overlap in subset for that id


df
   id      begin        end                       interval overlap
1   1 2010-01-31 2011-06-30 2010-01-31 UTC--2011-06-30 UTC    TRUE
2   1 2011-01-31 2012-06-30 2011-01-31 UTC--2012-06-30 UTC    TRUE
3   1 2012-01-31 2013-06-30 2012-01-31 UTC--2013-06-30 UTC    TRUE
4   1 2013-01-31 2014-06-30 2013-01-31 UTC--2014-06-30 UTC    TRUE
5   1 2013-02-28 2013-07-31 2013-02-28 UTC--2013-07-31 UTC    TRUE
6   1 2015-02-28 2015-03-31 2015-02-28 UTC--2015-03-31 UTC   FALSE
7   1 2015-06-30 2015-07-31 2015-06-30 UTC--2015-07-31 UTC   FALSE
8   1 2015-09-30 2016-01-31 2015-09-30 UTC--2016-01-31 UTC   FALSE
9   2 2010-01-31 2011-06-30 2010-01-31 UTC--2011-06-30 UTC    TRUE
10  2 2011-01-31 2012-06-30 2011-01-31 UTC--2012-06-30 UTC    TRUE
11  2 2012-01-31 2013-06-30 2012-01-31 UTC--2013-06-30 UTC    TRUE
12  2 2013-01-31 2014-06-30 2013-01-31 UTC--2014-06-30 UTC    TRUE
13  2 2013-02-28 2013-07-31 2013-02-28 UTC--2013-07-31 UTC    TRUE
14  2 2015-02-28 2015-03-31 2015-02-28 UTC--2015-03-31 UTC   FALSE
15  2 2015-06-30 2015-07-31 2015-06-30 UTC--2015-07-31 UTC   FALSE
16  2 2015-09-30 2016-01-31 2015-09-30 UTC--2016-01-31 UTC   FALSE