title: Collapse and merge overlapping time intervals
body: <p>I am developing a <code>tidyverse</code>-based data workflow, and came across a situation where I have a data frame with lots of time intervals. Let's call the data frame <code>my_time_intervals</code>, and it can be reproduced like this:</p>
  <pre><code>library(tidyverse)
  library(lubridate)
  
  my_time_intervals &lt;- tribble(
      ~id, ~group, ~start_time, ~end_time,
      1L, 1L, ymd_hms(&quot;2018-04-12 11:15:03&quot;), ymd_hms(&quot;2018-05-14 02:32:10&quot;),
      2L, 1L, ymd_hms(&quot;2018-07-04 02:53:20&quot;), ymd_hms(&quot;2018-07-14 18:09:01&quot;),
      3L, 1L, ymd_hms(&quot;2018-05-07 13:02:04&quot;), ymd_hms(&quot;2018-05-23 08:13:06&quot;),
      4L, 2L, ymd_hms(&quot;2018-02-28 17:43:29&quot;), ymd_hms(&quot;2018-04-20 03:48:40&quot;),
      5L, 2L, ymd_hms(&quot;2018-04-20 01:19:52&quot;), ymd_hms(&quot;2018-08-12 12:56:37&quot;),
      6L, 2L, ymd_hms(&quot;2018-04-18 20:47:22&quot;), ymd_hms(&quot;2018-04-19 16:07:29&quot;),
      7L, 2L, ymd_hms(&quot;2018-10-02 14:08:03&quot;), ymd_hms(&quot;2018-11-08 00:01:23&quot;),
      8L, 3L, ymd_hms(&quot;2018-03-11 22:30:51&quot;), ymd_hms(&quot;2018-10-20 21:01:42&quot;)
  )
  </code></pre>
  <p>Here's a <code>tibble</code> view of the same data frame:</p>
  <pre><code>&gt; my_time_intervals
  # A tibble: 8 x 4
       id group start_time          end_time           
    &lt;int&gt; &lt;int&gt; &lt;dttm&gt;              &lt;dttm&gt;             
  1     1     1 2018-04-12 11:15:03 2018-05-14 02:32:10
  2     2     1 2018-07-04 02:53:20 2018-07-14 18:09:01
  3     3     1 2018-05-07 13:02:04 2018-05-23 08:13:06
  4     4     2 2018-02-28 17:43:29 2018-04-20 03:48:40
  5     5     2 2018-04-20 01:19:52 2018-08-12 12:56:37
  6     6     2 2018-04-18 20:47:22 2018-04-19 16:07:29
  7     7     2 2018-10-02 14:08:03 2018-11-08 00:01:23
  8     8     3 2018-03-11 22:30:51 2018-10-20 21:01:42
  </code></pre>
  <p>A few notes about <code>my_time_intervals</code>:</p>
  <ol>
  <li><p>The data is divided into three groups via the <code>group</code> variable.</p>
  </li>
  <li><p>The <code>id</code> variable is just a unique ID for each row in the data frame.</p>
  </li>
  <li><p>The start and end of time intervals are stored in <code>start_time</code> and <code>end_time</code> in <code>lubridate</code> form.</p>
  </li>
  <li><p>Some time intervals overlap, some don't, and they are <em>not</em> always in order. For example, row <code>1</code> overlaps with row <code>3</code>, but neither of them overlaps with row <code>2</code>.</p>
  </li>
  <li><p>More than two intervals may overlap with each other, and some intervals fall completely within others. See rows <code>4</code> through <code>6</code> in <code>group == 2</code>.</p>
  </li>
  </ol>
  <p>What I want is that within each <code>group</code>, collapse any overlapping time intervals into contiguous intervals. In this case, my desired result would look like:</p>
  <pre><code># A tibble: 5 x 4
       id group start_time          end_time           
    &lt;int&gt; &lt;int&gt; &lt;dttm&gt;              &lt;dttm&gt;             
  1     1     1 2018-04-12 11:15:03 2018-05-23 08:13:06
  2     2     1 2018-07-04 02:53:20 2018-07-14 18:09:01
  3     4     2 2018-02-28 17:43:29 2018-08-12 12:56:37
  4     7     2 2018-10-02 14:08:03 2018-11-08 00:01:23
  5     8     3 2018-03-11 22:30:51 2018-10-20 21:01:42
  </code></pre>
  <p>Notice that time intervals that overlap <em>between</em> different <code>group</code>s are <em>not</em> merged. Also, I don't care about what happens to the <code>id</code> column at this point.</p>
  <p>I know that the <code>lubridate</code> package includes interval-related functions, but I can't figure out how to apply them to this use case.</p>
  <p>How can I achieve this?</p>