I have two dataframes:
d1 <- data.frame(id=1,a=1,start_d1=10, end_d1=19)
d2 <- data.frame(id=1,b=2, start_d2=15, end_d2=24)
Actually, start and end are dates and the data frames are huge, but I want to keep it simple here.
Now I want to join on id and then get new rows depending on where the respective start and end dates overlap. The result should be a dataframe d:
id | a | b | start | end
----------------------------
1 | 1 | NA | 10 | 14
1 | 1 | 2 | 15 | 19
1 | NA | 2 | 20 | 24
What I am doing right now: I join d1and d2 on id to get d.
Then I apply an explicit function I wrote to each row in d that splits the row into new ones with the new variables startand end depending on the overlap.
This is somewhat messy. Is there a simpler approach to my problem?