title: How to calculate time elapsed between rows in R
body: <p>I have date and time as separate columns, which i combined into a single column using library(lubridate)
  Now i want to create a new column that would calculate the elapsed time between two consecutive rows for each unique ID</p>
  
  <p>I tried diff, however the error i am getting is that the new column has +1 rows compared to original data set</p>
  
  <pre><code>s1$DT&lt;-with(s1, mdy(Date.of.Collection) + hm(MILITARY.TIME))#this worked - #needs the library lubridate
  s1$ElapsedTime&lt;-difff(s1$DT)
  units(s1$ElapsedTime)&lt;-"hours"
  </code></pre>
  
  <pre><code>Subject.ID  time    DT  Time elapsed
  1   Dose    8/1/2018 8:15   0
  1   time point1 8/1/2018 9:56   0.070138889
  1   time point2 8/2/2018 9:56   1.070138889
  2   Dose    9/4/2018 10:50  0
  2   time point1 9/11/2018 11:00 7.006944444
  3   Dose    10/1/2018 10:20 0
  3   time point1 10/2/2018 14:22 1.168055556
  3   time point2 10/3/2018 12:15 2.079861111
  </code></pre>