title: Find overlapping intervals in groups and retain largest non-overlapping periods
body: <p><strong>Issue</strong>
  I have a grouped dataframe with overlapping intervals (date as ymd). I want to retain only the largest non-overlapping intervals in each group.</p>
  <p><strong>Example data</strong></p>
  <pre><code># Packages
  library(tidyverse)
  library(lubridate)
  
  # Example data
  df &lt;- tibble(
    group = c(1, 1, 1, 2, 2, 3, 3, 3, 3),
    start = as_date(
      c(&quot;2019-01-10&quot;, &quot;2019-02-01&quot;, &quot;2019-10-05&quot;, &quot;2018-07-01&quot;, &quot;2019-01-01&quot;, &quot;2019-10-01&quot;, &quot;2019-10-01&quot;, &quot;2019-11-30&quot;,&quot;2019-11-20&quot;)),
    end = as_date(
      c(&quot;2019-02-07&quot;, &quot;2019-05-01&quot;, &quot;2019-11-15&quot;, &quot;2018-07-31&quot;, &quot;2019-05-05&quot;, &quot;2019-11-06&quot;, &quot;2019-10-07&quot;, &quot;2019-12-10&quot;,&quot;2019-12-31&quot;))) %&gt;%
    mutate(intval = interval(start, end),
           intval_length = intval / days(1))
  
  df
  #&gt; # A tibble: 9 x 5
  #&gt;   group start      end        intval                         intval_length
  #&gt;   &lt;dbl&gt; &lt;date&gt;     &lt;date&gt;     &lt;Interval&gt;                             &lt;dbl&gt;
  #&gt; 1     1 2019-01-10 2019-02-07 2019-01-10 UTC--2019-02-07 UTC            28
  #&gt; 2     1 2019-02-01 2019-05-01 2019-02-01 UTC--2019-05-01 UTC            89
  #&gt; 3     1 2019-10-05 2019-11-15 2019-10-05 UTC--2019-11-15 UTC            41
  #&gt; 4     2 2018-07-01 2018-07-31 2018-07-01 UTC--2018-07-31 UTC            30
  #&gt; 5     2 2019-01-01 2019-05-05 2019-01-01 UTC--2019-05-05 UTC           124
  #&gt; 6     3 2019-10-01 2019-11-06 2019-10-01 UTC--2019-11-06 UTC            36
  #&gt; 7     3 2019-10-01 2019-10-07 2019-10-01 UTC--2019-10-07 UTC             6
  #&gt; 8     3 2019-11-30 2019-12-10 2019-11-30 UTC--2019-12-10 UTC            10
  #&gt; 9     3 2019-11-20 2019-12-31 2019-11-20 UTC--2019-12-31 UTC            41
  
  # Goal
  # Row: 1 and 2; 6 to 9 have overlaps; Keep rows with largest intervals (in days)
  df1 &lt;- df[-c(1, 7, 8),]
  
  df1
  #&gt; # A tibble: 6 x 5
  #&gt;   group start      end        intval                         intval_length
  #&gt;   &lt;dbl&gt; &lt;date&gt;     &lt;date&gt;     &lt;Interval&gt;                             &lt;dbl&gt;
  #&gt; 1     1 2019-02-01 2019-05-01 2019-02-01 UTC--2019-05-01 UTC            89
  #&gt; 2     1 2019-10-05 2019-11-15 2019-10-05 UTC--2019-11-15 UTC            41
  #&gt; 3     2 2018-07-01 2018-07-31 2018-07-01 UTC--2018-07-31 UTC            30
  #&gt; 4     2 2019-01-01 2019-05-05 2019-01-01 UTC--2019-05-05 UTC           124
  #&gt; 5     3 2019-10-01 2019-11-06 2019-10-01 UTC--2019-11-06 UTC            36
  #&gt; 6     3 2019-11-20 2019-12-31 2019-11-20 UTC--2019-12-31 UTC            41
  </code></pre>
  <p><strong>Current approach</strong>
  I found a related question in another thread (see: <a href="https://stackoverflow.com/questions/44200364/find-dates-within-a-period-interval-by-group">Find dates within a period interval by group</a>). However, the respective solution identifies all overlapping rows by group. In this way, I can't identify the largest non-overlapping intervals.</p>
  <pre><code>df$overlap &lt;- unlist(tapply(df$intval, #loop through intervals
                              df$group,  #grouped by id
                              function(x) rowSums(outer(x,x,int_overlaps)) &gt; 1))
  </code></pre>
  <p>As an example, consider group 3 in my example data. Here row 6/7 and 8/9 overlap. With row 6 and 9 being the largest non-overlapping periods, I would like to remove row 7 and 8.</p>
  <p>I would greatly appreciate it if someone could pinpoint me to a solution.</p>