title: Calculating interval of time in sessions of users in R
body: <p>Im really new to Rstudio and i found a code that would help me analyze a dataset.
  Could anybody explain a little bit more detailed this line <code>replace_na(interval(lag(Date), Date)/dminutes(1), 0)</code> and also the meaning of <code> .add = T)</code> so that i can implement it in my code.</p>
  <pre><code>session_time_limit &lt;- 30
  df &lt;- tibble(ID = c(&quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;),
               Date = c(as.POSIXct(&quot;2021-01-25 19:17:12 UTC&quot;), #a1
                        as.POSIXct(&quot;2021-01-25 19:17:30 UTC&quot;), #a2
                        as.POSIXct(&quot;2021-01-25 19:57:12 UTC&quot;), #a3
                        as.POSIXct(&quot;2021-01-25 19:59:12 UTC&quot;), #a4
                        as.POSIXct(&quot;2021-01-25 20:11:12 UTC&quot;), #b1
                        as.POSIXct(&quot;2021-01-25 20:42:12 UTC&quot;), #b2
                        as.POSIXct(&quot;2021-01-25 21:15:42 UTC&quot;), #c1
                        as.POSIXct(&quot;2021-01-25 21:17:12 UTC&quot;), #c2
                        as.POSIXct(&quot;2021-01-25 21:20:13 UTC&quot;))) #c3
  
  df %&gt;% 
    group_by(ID) %&gt;%
    mutate(tdiff = replace_na(interval(lag(Date), Date)/dminutes(1), 0),
           session_number = cumsum(tdiff &gt; session_time_limit) + 1) %&gt;% 
    group_by(session_number, .add = T) %&gt;% 
    mutate(activity_within_session = row_number()) %&gt;% 
    ungroup()
  </code></pre>