title: Select row prior to first occurrence of an event by group
body: <p>I have a series of observations that describe if and when an animal is spotted in a specific area. The following sample table identifies when a certain animal is seen (<code>status == 1</code>) or not (<code>status == 0</code>) by day.</p>
  
  <pre><code>   id       date status
  1   1 2014-06-20      1
  2   1 2014-06-21      1
  3   1 2014-06-22      1
  4   1 2014-06-23      1
  5   1 2014-06-24      0
  6   2 2014-06-20      1
  7   2 2014-06-21      1
  8   2 2014-06-22      0
  9   2 2014-06-23      1
  10  2 2014-06-24      1
  11  3 2014-06-20      1
  12  3 2014-06-21      1
  13  3 2014-06-22      0
  14  3 2014-06-23      1
  15  3 2014-06-24      0
  16  4 2014-06-20      1
  17  4 2014-06-21      0
  18  4 2014-06-22      0
  19  4 2014-06-23      0
  20  4 2014-06-24      1
  </code></pre>
  
  <p>Using the <code>data.table</code> package, I can identify the first day an animal is no longer seen in the area:</p>
  
  <pre><code>library(data.table)
  dt &lt;- as.data.table(df)
  dt[status == 0, .SD[1], by = id]
    id       date status
  1:  1 2014-06-24      0
  2:  2 2014-06-22      0
  3:  3 2014-06-22      0
  4:  4 2014-06-21      0
  </code></pre>
  
  <p>While the above table is useful, I would like to know how to manipulate the function to find the dates prior to first occurrence of an animal's absence. In other words, I want to know the last day that each animal is in the area before temporarily leaving.  </p>
  
  <p>My actual data set bins these presence/absence observations into different time lengths depending on the situation (e.g. presence/absence by 3-hour intervals, 6-hour, etc). Therefore, it would be easier to access the previous row rather than subtract the time interval from each value since it always changes. My desired output would be the following:</p>
  
  <pre><code>  id       date status
  1:  1 2014-06-23      1
  2:  2 2014-06-21      1
  3:  3 2014-06-21      1
  4:  4 2014-06-20      1
  </code></pre>
  
  <p>Please feel free to use <code>base</code> code or other packages (i.e. <code>dplyr</code>) to answer this question, I am always up for something new. Thank you for your time!</p>