title: Is there a way to find the time difference within a single datetime column, grouped by ID?
body: <p>I have the following dataset:</p>
  
  <pre><code>                              ID      DATE
                                 A      12/18/2019 1:26:07 AM
                                 A      12/18/2019 1:26:30 AM
                                 A      12/18/2019 1:26:31 AM
                                 A      12/18/2019 1:26:32 AM
                                 A      12/18/2019 1:26:33 AM
                                 A      12/18/2019 1:26:34 AM
                                 A      12/18/2019 1:26:34 AM
                                 B      12/18/2019 1:26:37 AM
                                 B      12/18/2019 1:26:38 AM
  </code></pre>
  
  <p>Desired outcome:</p>
  
  <pre><code>                                 Duration of A 27 secs
                                   Duration of B  1 secs
  </code></pre>
  
  <p>I wish to accurately group by ID and find the time duration accurately.
  I have tried splitting the date time into starttime and endtime, but this is not
  yielding the correct result. I am not sure if I should create a loop?</p>
  
  <p>So far, I have entered this command, and am not getting the correct result:</p>
  
  <pre><code>        diff&lt;-data %&gt;% 
               mutate (Date = as.POSIXct(Date, format = "%m/%d/%Y %I:%M:%S %p")) %&gt;%
               group_by(ID) %&gt;% 
               summarise(timediff = difftime(first(DATE), last(DATE))
  </code></pre>
  
  <p>Any help is greatly appreciated.
  I will continue to research this.
  Thank you.</p>