title: Calculate total observation days in R (lubridate + dplyr)
body: <p>I am working with a dataset where each row contains a separate observation of an individual (ID). I would like R to look for the first and last observation dates and calculate the total number of "activity days", putting these values in a new column. When I run the code below, I get one column with the same number for every entry — the total number of days between the earliest and latest days of the entire data set, not per individual.</p>
  
  <p>I’m using dplyr and lubridate. I used this page (<a href="https://stackoverflow.com/questions/48673673/difference-between-the-first-date-and-last-date-within-same-individual-in-r">difference between the first date and last date within same individual in R</a>) for code assistance, but it isn't working for me.</p>
  
  <p>Can anyone help me figure out where I’m going wrong?</p>
  
  <pre><code>data$date &lt;- mdy(data$date)
  
  data &lt;- data %&gt;% 
    group_by(ID) %&gt;% 
    mutate(total.activity.days = max((date)) - min((date)))
  
     cage   date              ID   total.activity.days
  1   1     2018-04-30        40   54
  2   1     2018-04-30        76   54 
  3   1     2018-05-02        40   54 
  4   1     2018-05-02        76   54
  5   1     2018-05-04        40   54
  6   1     2018-05-04        76   54
  7   1     2018-04-28        59   54
  8   1     2018-04-29        59   54
  </code></pre>