Reputation: 693
I have a table with dateRanges and corresponding IDs. I want to group the IDs based on whether their start/end range overlaps with the date range for another ID. If a date range for an ID is partially or completely within that for another ID, they should belong to the same group. I want to add a column indicating this grouping, alongside the start/end date as given by the smallest and largest dates within the group.
The data:
"ID" "start" "end"
1 2018-10-02 2019-01-15
2 2019-01-13 2019-02-01
3 2018-10-01 2018-11-01
4 2018-10-05 2018-10-06
5 2019-09-09 2019-10-08
6 2019-02-06 2019-04-07
7 2019-03-24 2019-04-17
8 2019-03-21 2019-04-14
9 2019-03-27 2019-04-16
10 2019-04-30 2019-05-08
The ideal result:
"ID" "start" "end" "group_ID" "group_start" "group_end"
1 2018-10-02 2019-01-15 1 2018-10-01 2019-02-01
2 2019-01-13 2019-02-01 1 2018-10-01 2019-02-01
3 2018-10-01 2018-11-01 1 2018-10-01 2019-02-01
4 2018-10-05 2018-10-06 1 2018-10-01 2019-02-01
5 2019-09-09 2019-10-08 2 2019-09-09 2019-10-08
6 2019-02-06 2019-04-07 3 2019-02-06 2019-05-08
7 2019-03-24 2019-04-17 3 2019-02-06 2019-05-08
8 2019-03-21 2019-04-14 3 2019-02-06 2019-05-08
9 2019-03-27 2019-04-16 3 2019-02-06 2019-05-08
10 2019-04-30 2019-05-08 3 2019-02-06 2019-05-08
What I've been thinking of that may work is creating a matrix of IDs (i.e.- rows and columns spanning from ID 1 to ID 10) and filling each cell on whether the date ranges for the given intersection of IDs overlap. Following this, binning then into groups and finding the min/max for the given group, but this seems really complicated. There must be an easier solution that does not involve looking at edges on a matrix to create clusters.
Edit- format for .csv:
ID,start,end
1,2018-10-02,2019-01-15
2,2019-01-13,2019-02-01
3,2018-10-01,2018-11-01
4,2018-10-05,2018-10-06
5,2019-09-09,2019-10-08
6,2019-02-06,2019-04-07
7,2019-03-24,2019-04-17
8,2019-03-21,2019-04-14
9,2019-03-27,2019-04-16
10,2019-04-30,2019-05-08
Upvotes: 3
Views: 349
Reputation: 25225
Here is an option:
setorder(DT, start, end)
DT[order(start, end), g := cumsum(start > shift(cummax(as.integer(end)), fill=0L))][,
c("gstart","gend") := .(min(start), max(end)), g]
output:
ID start end g gstart gend
1: 1 2018-10-02 2019-01-15 1 2018-10-01 2019-02-01
2: 2 2019-01-13 2019-02-01 1 2018-10-01 2019-02-01
3: 3 2018-10-01 2018-11-01 1 2018-10-01 2019-02-01
4: 4 2018-10-05 2018-10-06 1 2018-10-01 2019-02-01
5: 5 2019-09-09 2019-10-08 4 2019-09-09 2019-10-08
6: 6 2019-02-06 2019-04-07 2 2019-02-06 2019-04-17
7: 7 2019-03-24 2019-04-17 2 2019-02-06 2019-04-17
8: 8 2019-03-21 2019-04-14 2 2019-02-06 2019-04-17
9: 9 2019-03-27 2019-04-16 2 2019-02-06 2019-04-17
10: 10 2019-04-30 2019-05-08 3 2019-04-30 2019-05-08
data:
library(data.table)
DT <- fread("ID,start,end
1,2018-10-02,2019-01-15
2,2019-01-13,2019-02-01
3,2018-10-01,2018-11-01
4,2018-10-05,2018-10-06
5,2019-09-09,2019-10-08
6,2019-02-06,2019-04-07
7,2019-03-24,2019-04-17
8,2019-03-21,2019-04-14
9,2019-03-27,2019-04-16
10,2019-04-30,2019-05-08")
cols <- c("start", "end")
DT[, (cols) := lapply(.SD, as.IDate, format="%Y-%m-%d"), .SDcols=cols]
Reference: How to flatten / merge overlapping time periods
Upvotes: 0