I'm trying to create new rows based on overlapping time periods of existing rows. For example, I'd like to turn this:
Customer_Product <- data.table(Customer=c("A01","A01","A01", "A02", "A02", "A02", "A03", "A03", "A03"),
Product=c("Prod1","Prod2","Prod3","Prod1","Prod2","Prod3","Prod1","Prod2","Prod3"),
Start_Date=c("1/1/2015", "3/1/2015", "4/1/2015", "1/1/2015", "3/1/2015", "4/1/2015", "1/1/2015", "3/1/2015", "4/1/2015"),
End_Date=c("2/1/2015","5/1/2015","5/1/2015","2/1/2015","5/1/2015","6/1/2015","2/1/2015","6/1/2015","5/1/2015"))
Customer Product Start_Date End_Date 1: A01 Prod1 1/1/2015 2/1/2015 2: A01 Prod2 3/1/2015 5/1/2015 3: A01 Prod3 4/1/2015 5/1/2015 4: A02 Prod1 1/1/2015 2/1/2015 5: A02 Prod2 3/1/2015 5/1/2015 6: A02 Prod3 4/1/2015 6/1/2015 7: A03 Prod1 1/1/2015 2/1/2015 8: A03 Prod2 3/1/2015 6/1/2015 9: A03 Prod3 4/1/2015 5/1/2015
Into something like this:
Customer_Product_Combo <- data.table(Customer=c("A01","A01","A01", "A02", "A02", "A02", "A02","A03", "A03","A03","A03"),
Product_or_Combination=c("Prod1","Prod2","Prod2/Prod3","Prod1","Prod2","Prod2/Prod3","Prod3","Prod1","Prod2","Prod2/Prod3","Prod2"),
Start_Date=c("1/1/2015","3/1/2015","4/1/2015","1/1/2015","3/1/2015","4/1/2015","5/1/2015","1/1/2015","3/1/2015","4/1/2015","5/1/2015"),
End_Date=c("2/1/2015","4/1/2015","5/1/2015","2/1/2015","4/1/2015","5/1/2015","6/1/2015","2/1/2015","4/1/2015","5/1/2015","6/1/2015"))
Customer Product_or_Combination Start_Date End_Date 1: A01 Prod1 1/1/2015 2/1/2015 2: A01 Prod2 3/1/2015 4/1/2015 3: A01 Prod2/Prod3 4/1/2015 5/1/2015 4: A02 Prod1 1/1/2015 2/1/2015 5: A02 Prod2 3/1/2015 4/1/2015 6: A02 Prod2/Prod3 4/1/2015 5/1/2015 7: A02 Prod3 5/1/2015 6/1/2015 8: A03 Prod1 1/1/2015 2/1/2015 9: A03 Prod2 3/1/2015 4/1/2015 10: A03 Prod2/Prod3 4/1/2015 5/1/2015 11: A03 Prod2 5/1/2015 6/1/2015
I've been looking into IRanges, because it seems like disjoin() may be a possible solution, but I can't see any way to inherit/merge the "Prod" data.
I've also been trying to sketch out something using lead/lag in dplyr followed by a gather/merge cycle, but it's also worth noting that I could have instances where more than 2 "Prod"s overlap, and then the logic just gets messy.
Is there a reasonable way to do this? Any help is greatly appreciated!