title: Time interval over multiple rows
body: <p>I'm trying to solve a problem working with time intervals that I gives me headaches since some time. </p>
  
  <p>This an example of drug prescriptions for two IDs.</p>
  
  <pre><code>df &lt;- data.frame('ID' = c('1','1','1','1','2','2'), 'start' = c('2010-01-01', '2010-01-03', '2010-01-05', '2010-01-09','2010-02-01', '2010-02-10'),
               'end' = c('2010-01-03', '2010-01-22', '2010-01-07', '2010-01-12', '2010-02-10', '2010-02-12'))
  
    ID      start        end
  1  1 2010-01-01 2010-01-03
  2  1 2010-01-03 2010-01-22
  3  1 2010-01-05 2010-01-07
  4  1 2010-01-09 2010-01-12
  5  2 2010-02-01 2010-02-10
  6  2 2010-02-10 2010-02-12
  </code></pre>
  
  <p>The aim is to create a new column indicating continuous drug use. So in this example both IDs have a continuous use (maxmimum gap between end of prescription and start of next prescription = 1 day). The final result should look like this (logical to indicate continuous use):</p>
  
  <pre><code>  ID      start        end  continuous
  1  1 2010-01-01 2010-01-03 FALSE
  2  1 2010-01-03 2010-01-22 TRUE
  3  1 2010-01-05 2010-01-07 TRUE
  4  1 2010-01-09 2010-01-12 TRUE
  5  2 2010-02-01 2010-02-10 FALSE
  6  2 2010-02-10 2010-02-12 TRUE
  </code></pre>
  
  <p>I tried to solve this with dplyr and lubridate but didn't manage to get the right result for row 4 as it is not continuous for row 3 but for row 2. Here is my code:</p>
  
  <pre><code>df$start &lt;- ymd(df$start)
  df$end &lt;- ymd(df$end)
  df &lt;- df %&gt;% group_by(ID) %&gt;% mutate(continuous = if_else(lag(end) - start &gt;= -1, TRUE, FALSE, missing = FALSE))
  </code></pre>
  
  <p>I would really appreciate some help with this! Thx!</p>