title: calculate time difference by group using data.table get the units messed up
body: <p>The objective is to calculate the time between events grouped by some id. Here is an example:</p>
  
  <pre class="lang-r prettyprint-override"><code>library(data.table)
  library(lubridate)
  
  dt &lt;- data.table(id = c(1,1:3), 
                   start = c("2015-01-01 12:00:00", "2015-12-01 12:00:00", "2019-01-01 12:00:00", NA),
                   end = c("2016-01-01 12:00:01", "2016-01-01 12:00:01", "2019-01-01 12:00:01", "2019-01-01 12:00:02"))
  
  dt[, start := ymd_hms(start)]
  dt[, end := ymd_hms(end)]
  
  dt[, time_diff_1 := min(end) - max(start), by = .(id)]
  dt[, time_diff_2 := end - start]
  </code></pre>
  
  <p>which results in:</p>
  
  <pre class="lang-r prettyprint-override"><code>   id               start                 end   time_diff_1   time_diff_2
  1:  1 2015-01-01 12:00:00 2016-01-01 12:00:01 31.00001 secs 31536001 secs
  2:  1 2015-12-01 12:00:00 2016-01-01 12:00:01 31.00001 secs  2678401 secs
  3:  2 2019-01-01 12:00:00 2019-01-01 12:00:01  1.00000 secs        1 secs
  4:  3                &lt;NA&gt; 2019-01-01 12:00:02       NA secs       NA secs
  </code></pre>
  
  <p>Both columns <code>time_diff_1</code> and <code>time_diff_2</code> display the time difference in seconds. However the <code>time_diff_1</code> which resulted from the grouped calculation mixed up the units. The result for <code>id == 1</code> is 31 days and one second. It seems as if the units were choosen automatically by group and then gotten overwritten.</p>
  
  <p>Any hints on how to fix this?</p>