我有一个包含产品和区间的数据框:
df <- data.frame(Product=c("x","y","z", "x"),
Start_Date=c("1/1/2015 08:00", "3/1/2015 10:00", "4/1/2015 09:00", "4/1/2015 12:34"),
End_Date=c("2/1/2015 09:00","5/1/2015 12:00","5/1/2015 13:00", "7/1/2015 11:23"))
Product Start_Date End_Date
1 x 1/1/2015 08:00 2/1/2015 09:00
2 y 3/1/2015 10:00 5/1/2015 12:00
3 z 4/1/2015 09:00 5/1/2015 13:00
4 x 4/1/2015 12:34 7/1/2015 11:23
我希望找出产品的日期和时间之间的重叠部分,将它们拆分成互斥集合,并记录每个区间发生的产品。
最终结果应类似于这样:
df2 <- data.frame(Product=c("x","y", "y/z", "y/z/x", "z/x", "x"),
Start_Date=c("1/1/2015 08:00", "3/1/2015 10:00", "4/1/2015 09:00", "4/1/2015 12:34","5/1/2015
12:00", "5/1/2015 13:00"),
End_Date=c("2/1/2015 09:00", "4/1/2015 09:00", "4/1/2015 12:34", "5/1/2015 12:00", "5/1/2015
13:00", "7/1/2015 11:23"))
Product Start_Date End_Date
1 x 1/1/2015 08:00 2/1/2015 09:00
2 y 3/1/2015 10:00 4/1/2015 09:00
3 y/z 4/1/2015 09:00 4/1/2015 12:34
4 y/z/x 4/1/2015 12:34 5/1/2015 12:00
5 z/x 5/1/2015 12:00 5/1/2015 13:00
6 x 5/1/2015 13:00 7/1/2015 11:23
IRanges
。点赞! - ThomasIsCoding