title: adding rows for missing intervals between existing intervals in R
body: <p><strong>Problem</strong>:
  I have a data set that contains interval data with start and end dates and my goal is it to add rows to that data set that reflect the potential gaps between these intervals (per group). The complexity is that the already existing intervals may overlap and there might be an arbitrary number of gaps.</p>
  <p><strong>Example</strong>: We have start and end dates showing from when to when a person has occupied an apartment. There may have been multiple people at one apartment at the same time and there needs to be rows added for the time the apartment has been &quot;empty&quot;.</p>
  <p><strong>Example data</strong>:</p>
  <pre><code>example_dates &lt;- structure(list(apartment = c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, 
                                                  &quot;B&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;), 
                                  start_date = structure(c(1640995200, 1642291200, 
                                                           1649980800, 1655769600, 1644451200, 1646092800, 1659312000, 1646438400, 
                                                           1649376000, 1664582400), 
                                                         class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;), tzone = &quot;UTC&quot;), 
                                  end_date = structure(c(1642204800, 1648166400, 1655683200, 
                                                         1668643200, 1655251200, 1653868800, 1667260800, 1654819200, 
                                                         1661385600, 1668470400), class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;), tzone = &quot;UTC&quot;), 
                                  status = c(&quot;person in apartment&quot;, &quot;person in apartment&quot;, 
                                             &quot;person in apartment&quot;, &quot;person in apartment&quot;, &quot;person in apartment&quot;, 
                                             &quot;person in apartment&quot;, &quot;person in apartment&quot;, &quot;person in apartment&quot;, 
                                             &quot;person in apartment&quot;, &quot;person in apartment&quot;)), 
                             class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -10L))
  </code></pre>
  <p><strong>Desired output</strong>:</p>
  <pre><code>desired_outcome &lt;- structure(list(apartment = c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, 
                                                    &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;), 
                                    start_date = structure(c(1640995200, 1642291200, 1648252800, 
                                                             1649980800, 1655769600, 1644451200, 1646092800, 
                                                             1655337600, 1659312000, 1646438400, 1649376000, 1661472000, 1664582400), 
                                                           class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;), tzone = &quot;UTC&quot;), 
                                    end_date = structure(c(1642204800, 1648166400, 1649894400, 1655683200, 1668643200, 1655251200, 1653868800, 
                                                           1659225600, 1667260800, 1654819200, 1661385600, 1664496000, 1668470400),
                                                         class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;), tzone = &quot;UTC&quot;), 
                                    status = c(&quot;person in apartment&quot;, &quot;person in apartment&quot;, &quot;apartment empty&quot;, 
                                               &quot;person in apartment&quot;, &quot;person in apartment&quot;, &quot;person in apartment&quot;, 
                                               &quot;person in apartment&quot;, &quot;apartment empty&quot;, &quot;person in apartment&quot;, &quot;person in apartment&quot;, 
                                               &quot;person in apartment&quot;, &quot;apartment empty&quot;, &quot;person in apartment&quot;
                                                    )), 
                               class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -13L))
  </code></pre>