I want to merge two data frames. Both of them have a begin date and an end date.
If the given intervals are overlapping, I want to split the resulting rows in non overlapping intevals.
Please see this example:
a
id beg_a end_a prop_a
1 2000-01-01 2002-12-31 A
2 2000-01-01 2000-02-15 B
2 2000-04-01 2000-04-15 A
2 2002-01-01 2002-12-31 B
3 2000-01-01 2000-06-15 A
b
id beg_b end_b prop_b
1 1999-06-01 2000-05-15 D
1 2003-01-15 2003-01-31 D
2 1999-01-01 2003-01-15 D
3 2000-07-01 2001-08-01 E
merged
id beg_a end_a prop_a beg_b end_b prop_b overallBeg overallEnd
1 <NA> <NA> <NA> 1999-06-01 2000-05-15 D 1999-06-01 1999-12-31
1 2000-01-01 2002-12-31 A 1999-06-01 2000-05-15 D 2000-01-01 2000-05-15
1 2000-01-01 2002-12-31 A <NA> <NA> <NA> 2000-05-16 2002-12-31
1 <NA> <NA> <NA> 2003-01-15 2003-01-31 D 2003-01-15 2003-01-31
2 <NA> <NA> <NA> 1999-01-01 2003-01-15 D 1999-01-01 1999-12-31
2 2000-01-01 2000-02-15 B 1999-01-01 2003-01-15 D 2000-01-01 2000-02-15
2 <NA> <NA> <NA> 1999-01-01 2003-01-15 D 2000-02-16 2000-03-31
2 2000-04-01 2000-04-15 A 1999-01-01 2003-01-15 D 2000-04-01 2000-04-15
2 <NA> <NA> <NA> 1999-01-01 2003-01-15 D 2000-04-16 2001-12-31
2 2002-01-01 2002-12-31 B 1999-01-01 2003-01-15 D 2002-01-01 2002-12-31
2 <NA> <NA> <NA> 1999-01-01 2003-01-15 D 2003-01-01 2003-01-15
3 2000-01-01 2000-06-15 A <NA> <NA> <NA> 2000-01-01 2000-06-15
3 <NA> <NA> <NA> 2000-07-01 2001-08-01 E 2000-07-01 2001-08-01
(or simply use these commands in R)
a <- structure(list(id = c(1, 2, 2, 2, 3), beg_a = structure(c(10957,
10957, 11048, 11688, 10957), class = "Date"), end_a = structure(c(12052,
11002, 11062, 12052, 11123), class = "Date"), prop_a = structure(c(1L,
2L, 1L, 2L, 1L), .Label = c("A", "B"), class = "factor")), .Names = c("id",
"beg_a", "end_a", "prop_a"), row.names = c(NA, -5L), class = "data.frame")
b <- structure(list(id = c(1, 1, 2, 3), beg_b = structure(c(10743,
12067, 10592, 11139), class = "Date"), end_b = structure(c(11092,
12083, 12067, 11535), class = "Date"), prop_b = structure(c(1L,
1L, 1L, 2L), .Label = c("D", "E"), class = "factor")), .Names = c("id",
"beg_b", "end_b", "prop_b"), row.names = c(NA, -4L), class = "data.frame")
merged <- structure(list(id = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3),
beg_a = structure(c(NA, 10957, 10957, NA, NA, 10957, NA,
11048, NA, 11688, NA, 10957, NA), class = "Date"), end_a = structure(c(NA,
12052, 12052, NA, NA, 11002, NA, 11062, NA, 12052, NA, 11123,
NA), class = "Date"), prop_a = structure(c(NA, 1L, 1L, NA,
NA, 2L, NA, 1L, NA, 2L, NA, 1L, NA), .Label = c("A", "B"), class = "factor"),
beg_b = structure(c(10743, 10743, NA, 12067, 10592, 10592,
10592, 10592, 10592, 10592, 10592, NA, 11139), class = "Date"),
end_b = structure(c(11092, 11092, NA, 12083, 12067, 12067,
12067, 12067, 12067, 12067, 12067, NA, 11535), class = "Date"),
prop_b = structure(c(1L, 1L, NA, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, NA, 2L), .Label = c("D", "E"), class = "factor"),
overallBeg = structure(c(10743, 10957, 11093, 12067, 10592,
10957, 11003, 11048, 11063, 11688, 12053, 10957, 11139), class = "Date"),
overallEnd = structure(c(10956, 11092, 12052, 12083, 10956,
11002, 11047, 11062, 11687, 12052, 12067, 11123, 11535), class = "Date")), .Names = c("id",
"beg_a", "end_a", "prop_a", "beg_b", "end_b", "prop_b", "overallBeg",
"overallEnd"), row.names = c(NA, -13L), class = "data.frame")
I think there are some similarities with another question of mine: "smoothing" time data - can it be done more efficient?
But also slightly different.
Thank you in advance for your help!