title: Remove duplicate entries given certain criteria using the tidyverse
body: <p>Consider the following example data:</p>
  
  <pre class="lang-r prettyprint-override"><code>library(dplyr)
  d &lt;- tibble("ID" = rep(c(1111, 2222, 3333, 4444), each = 4),
              "DAY" = c(1, 2, 3, 4, 1, 2, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4),
              "FINISHED" = c(1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0))
  </code></pre>
  
  <p>I want to remove one entry from participants (<code>ID</code>) who completed a given day's survey (<code>DAY</code>) twice. Specifically I want to remove that row (or rows, if completed 3+ times in a given day) where they did not finish the entire survey (<code>FINISHED == 0</code>).</p>
  
  <p>Is this possible to do with <code>group_by</code>? Any <code>tidyverse</code> solution is welcome.</p>
  
  <p><strong>Expected output</strong> (row 7 removed):</p>
  
  <pre class="lang-r prettyprint-override"><code># A tibble: 16 x 3
        ID   DAY FINISHED
     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
   1  1111     1        1
   2  1111     2        1
   3  1111     3        1
   4  1111     4        1
   5  2222     1        1
   6  2222     2        1
   8  2222     3        1
   9  3333     1        1
  10  3333     2        1
  11  3333     3        1
  12  3333     4        1
  13  4444     1        1
  14  4444     2        1
  15  4444     3        1
  16  4444     4        0
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>If a participant takes a given day's survey 2+ times and finishes both (<code>FINISH == 1</code>), I would like to remove the earliest completion (i.e., keep the date closest to today). Here's an extended example dataset:</p>
  
  <pre class="lang-r prettyprint-override"><code>library(dplyr)
  library(lubridate)
  d &lt;- tibble("ID" = rep(c(1111, 2222, 3333, 4444), each = 4),
              "DAY" = c(1, 2, 3, 3, 1, 2, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4),
              "FINISHED" = c(1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0),
              "DATE" = as_date(c("2019-08-01", "2019-08-02", "2019-08-03", "2019-08-04",
                         "2019-08-01", "2019-08-02", "2019-08-02", "2019-08-03",
                         "2019-08-01", "2019-08-02", "2019-08-03", "2019-08-04",
                         "2019-08-01", "2019-08-02", "2019-08-03", "2019-08-04")))
  </code></pre>
  
  <p>And the expected output (rows 3, 7 removed):</p>
  
  <pre class="lang-r prettyprint-override"><code># A tibble: 16 x 4
        ID   DAY FINISHED DATE      
     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt; &lt;date&gt;    
   1  1111     1        1 2019-08-01
   2  1111     2        1 2019-08-02
   4  1111     3        1 2019-08-04
   5  2222     1        1 2019-08-01
   6  2222     2        1 2019-08-02
   8  2222     3        1 2019-08-03
   9  3333     1        1 2019-08-01
  10  3333     2        1 2019-08-02
  11  3333     3        1 2019-08-03
  12  3333     4        1 2019-08-04
  13  4444     1        1 2019-08-01
  14  4444     2        1 2019-08-02
  15  4444     3        1 2019-08-03
  16  4444     4        0 2019-08-04
  </code></pre>