title: Dplyr/Lubridate: How to summarise overlapping intervals after grouping
body: <p>I would like to group agreements and then compare how much their periods overlap (or are apart).</p>
  
  <p>My dataframe may look like:</p>
  
  <pre><code>library(tidyverse)
  library(lubridate)
  
  tribble(
  ~ShipTo,    ~Code,  ~Start, ~End,
  "xxxx", "AAA11",    2018-01-01, 2018-03-01,
  "yyyy", "BBB23",    2018-02-01, 2018-05-11,
  "yyyy", "BBB23",    2018-03-01, 2018-06-11,
  "cccc", "AAA11",    2018-01-06, 2018-03-12,
  "yyyy", "CCC04",    2018-01-16, 2018-03-31,
  "xxxx", "DDD",    2018-01-21,   2018-03-25
  )
  </code></pre>
  
  <p>I would like to mutate a column to create lubridate periods and evaluate them after grouping by ShipTo and Code. What I tried was:</p>
  
  <pre><code>dft3&lt;-dft %&gt;% filter(concat1 %in% to_filter2)  %&gt;%
    arrange(ShipTo,Code)%&gt;% 
    group_by(ShipTo,Code)%&gt;%
    mutate(period=interval(Start,End), 
           nextperiod=interval(lead(Start),lead(End)),
           interv=day(as.period(intersect(period, nextperiod), "days"))) %&gt;%
    group_by(ShipTo,Code)%&gt;%
    summarise(count=n(),
      intervmax=max(interv),
      intervmin=min(interv)) 
  </code></pre>
  
  <p>If I remove the line group_by(ShipTo,Code)%>% the intervals are created correctly and also the lead intervals are correctly calculated from the next line. But when I naively use group_by, the intervals are not calculated correctly.</p>
  
  <p>I suspect that perhaps my database should be split into many tables by groups and then, after the operation of creating and comparing intervals it should be glued back together.</p>
  
  <p>Is there a succinct way to do it? Or perhaps there is a simpler way I have not yet learned? Thank you in advance for the hint in the right direction.</p>
  
  <p>EDIT: The desired output should be a column with value of overlaps of intervals in days (or distances between intervals if no overlap). Grouping destroys the calculation. I would like to have these values calculated within groups (not accross them).</p>
  
  <p>EDIT2: I trying to solve the problem by splitting dataframe into a list of dataframes and then combining it, but I am not sure of a syntax. It does not quite work, produces tables with one column, a help I was given on other portal (perhaps it can ilustrate the issue). The idea is to split a database, create new columns and combine the tables to a single table.</p>
  
  <pre><code>    fnOverlaps &lt;- function(x) {
  
        mutate(x,okres=interval(Start,End),
               nastokres=interval(lead(Start),lead(End)), 
               interv=day(as.period(intersect(okres, nastokres), "days"))) 
      }
  
  dft3&lt;-dft3 %&gt;% 
    split(list(.$ShipTo, .$Code), drop = TRUE)  %&gt;%   
    map_df(fnOverlaps) %&gt;% 
    flatten_dfr()
  </code></pre>
  
  <p>The result (for one group) that I expect would look like this.</p>
  
  <pre><code>tribble(
  ~ShipTo,    ~Code,  ~interv,    
  "yyyy", "BBB23",    70        #say there is a 70 days overlap
  "yyyy", "BBB23",    NA        #there is no next row to compare
  
  )
  </code></pre>