title: How to calculate time difference in consecutive rows
body: <p>Raw data looks like this and I want to sort it by visitor and time, to calculate the time difference in the rows, before saving it to a new file.</p>
  
  <pre><code>  visitor         v_time payment items
  1    Jack 1/2/2018 16:07      35     3
  2    Jack 1/2/2018 16:09     160     1
  3   David 1/2/2018 16:12      25     2
  4    Kate 1/2/2018 16:16       3     3
  5   David 1/2/2018 16:21      25     5
  6    Jack 1/2/2018 16:32      85     5
  7    Kate 1/2/2018 16:33     639     3
  8    Jack 1/2/2018 16:55       6     2
  </code></pre>
  
  <p>The grouping and sorting are ok. But it failed to calculate the time difference, nor the file saving.</p>
  
  <pre><code>visitor &lt;- c("Jack", "Jack", "David", "Kate", "David", "Jack", "Kate", "Jack")
  v_time &lt;- c("1/2/2018 16:07","1/2/2018 16:09","1/2/2018 16:12","1/2/2018 16:16","1/2/2018 16:21","1/2/2018 16:32","1/2/2018 16:33", "1/2/2018 16:55")
  payment &lt;- c(35,160,25,3,25,85,639,6)
  items &lt;- c(3,1,2,3,5,5,3,2)
  df &lt;- data.frame(visitor, v_time, payment, items)
  
  df %&gt;%
    arrange(visitor, v_time) %&gt;%
    group_by(visitor) %&gt;%
    mutate(diff = strptime(v_time, "%d/%m/%Y %H:%M") - lag(strptime(v_time, "%d/%m/%Y %H:%M")), diff_secs = as.numeric(diff, units = 'secs'))
  
  write.csv(df,"C:/output.csv", row.names = F)
  </code></pre>
  
  <p>What is my error and the right way of doing it?</p>
  
  <pre><code># A tibble: 8 x 6
  # Groups: visitor [3]
    visitor v_time         payment items diff   diff_secs
    &lt;fct&gt;   &lt;fct&gt;            &lt;dbl&gt; &lt;dbl&gt; &lt;time&gt;     &lt;dbl&gt;
  1 David   1/2/2018 16:12   25.0   2.00 NA            NA
  2 David   1/2/2018 16:21   25.0   5.00 NA            NA
  3 Jack    1/2/2018 16:07   35.0   3.00 NA            NA
  4 Jack    1/2/2018 16:09  160     1.00 NA            NA
  5 Jack    1/2/2018 16:32   85.0   5.00 NA            NA
  6 Jack    1/2/2018 16:55    6.00  2.00 NA            NA
  7 Kate    1/2/2018 16:16    3.00  3.00 NA            NA
  8 Kate    1/2/2018 16:33  639     3.00 NA            NA
  </code></pre>