title: calculating followup per id
body: <p>I'm trying to recalculate follow-up time in years for a medium size data frame (16000 rows 100 variables).</p>
  
  <p>This has been partially asked here 
  <a href="https://stackoverflow.com/questions/30378946/calculating-time-difference-by-id">Calculating time difference by ID</a> 
  but what is described here is in essence what I want to do, but I don't have time only date available. </p>
  
  <p>I would like to figure out why it does not work for only date.</p>
  
  <p>when I run this code it works:</p>
  
  <h1>generate data:</h1>
  
  <pre><code>Incident.ID.. = c(rep("INCFI0000029582",4), rep("INCFI0000029587",4))
  date = c("2014-09-25 08:39:45", "2014-09-25 08:39:48", "2014-09-25 08:40:44", "2014-10-10 23:04:00", "2014-09-25 08:33:32", "2014-09-25 08:34:41", "2014-09-25 08:35:24", "2014-10-10 23:04:00")
  df = data.frame(Incident.ID..,date, stringsAsFactors = FALSE)
  </code></pre>
  
  <h1>run code</h1>
  
  <pre><code>library(dplyr)
  library(lubridate)
  df %&gt;%
    group_by(Incident.ID..) %&gt;%
    mutate(diff = c(0, diff(ymd_hms(date))))
  </code></pre>
  
  <h1>output</h1>
  
  <pre><code>  Incident.ID..   date                    diff
    &lt;chr&gt;           &lt;chr&gt;                  &lt;dbl&gt;
  1 INCFI0000029582 2014-09-25 08:39:45       0.
  2 INCFI0000029582 2014-09-25 08:39:48       3.
  3 INCFI0000029582 2014-09-25 08:40:44      56.
  4 INCFI0000029582 2014-10-10 23:04:00 1347796.
  5 INCFI0000029587 2014-09-25 08:33:32       0.
  6 INCFI0000029587 2014-09-25 08:34:41      69.
  7 INCFI0000029587 2014-09-25 08:35:24      43.
  8 INCFI0000029587 2014-10-10 23:04:00 1348116.
  </code></pre>
  
  <p>my data has a YMD format so this seemed logical:</p>
  
  <h1>generate data</h1>
  
  <pre><code>Incident.ID.. = c(rep("INCFI0000029582",4), rep("INCFI0000029587",4))
  date = c("2014-09-20", "2014-09-21", "2014-09-22", "2014-09-23", "2014-09-26", "2014-09-27", "2014-09-28", "2014-10-10")
  df = data.frame(Incident.ID..,date, stringsAsFactors = FALSE)
  </code></pre>
  
  <h1>run code</h1>
  
  <pre><code>library(dplyr)
  library(lubridate)
  df %&gt;%
    group_by(Incident.ID..) %&gt;%
    mutate(diff = c(0, diff(ymd(date))))
  </code></pre>
  
  <h1>output</h1>
  
  <pre><code>  Incident.ID..   date        diff
    &lt;chr&gt;           &lt;chr&gt;      &lt;dbl&gt;
  1 INCFI0000029582 2014-09-20    0.
  2 INCFI0000029582 2014-09-21    1.
  3 INCFI0000029582 2014-09-22    1.
  4 INCFI0000029582 2014-09-23    1.
  5 INCFI0000029587 2014-09-26    0.
  6 INCFI0000029587 2014-09-27    1.
  7 INCFI0000029587 2014-09-28    1.
  8 INCFI0000029587 2014-10-10   12.
  </code></pre>
  
  <p>it now gives differences between subsequent observations instead of the first Incident.ID..</p>
  
  <p>an answer was given here: <a href="https://stackoverflow.com/questions/43284810/date-difference-per-id-with-dplyr-without-hours-minutes-or-seconds">Date difference per ID with dplyr without hours, minutes or seconds</a> but I still don't understand WHY it does not work using this very similar code above. (any why it now gives the difference in days instead of seconds).</p>
  
  <p>when using data table method I get the same result:</p>
  
  <pre><code>padded.diff = function(x) c(0L, diff(x)) 
  library(data.table) 
  setDT(df)[, date.diff := padded.diff(as.POSIXct(date)), by = Incident.ID..]
  View(df)
  </code></pre>
  
  <p>Any explanation of this behaviour would be very welcome as it does not give a parsing error . (it probably has something to do with the posix time but I'm baffled)</p>