title: R: filter duplicates/rows based on two conditions
body: <p>I'm working in R and I need to filter duplicates from a large dataset based on two conditions:</p>
  <p><em>In principle, retain the record with the first date of diagnosis UNLESS a later date included a negative diagnosis - in which case retain the record with the negative diagnosis.</em></p>
  <p>Here's a test dataframe where patients 12 and 13 are duplicated:</p>
  <pre><code>patient &lt;- c('11', '12', '12', '13', '13', '14')
  diagnosis &lt;- c('positive', 'positive', 'negative', 'positive', 'positive', 'negative')
  date &lt;- c('01-01-2020', '02-02-2020', '03-03-2020', '04-04-2020', '05-05-2020', '06-06-2020') %&gt;% as.Date(&quot;%d-%m-%y&quot;)
  
  data &lt;- data.frame(patient, diagnosis, date)
  
  print(data)
  
  patient diagnosis date
  11      positive  2020-01-01
  12      positive  2020-02-02
  12      negative  2020-03-03
  13      positive  2020-04-04
  13      positive  2020-05-05
  14      negative  2020-06-06
  
  </code></pre>
  <p>I need to get the following result where patient 12's negative diagnosis at a later date is retained and patient 13's first diagnosis was retained:</p>
  <pre><code>patient diagnosis date
  11      positive  2020-01-01
  12      negative  2020-03-03
  13      positive  2020-04-04
  14      negative  2020-06-06
  
  </code></pre>
  <p>My solution starts off as:</p>
  <pre><code>data &lt;- group_by(data, patient) %&gt;%     #group by patient number
          arrange(date, .by_group = TRUE) #arrange by date in ascending order
  </code></pre>
  <p>I'm not sure how to proceed from here. If I only needed the first date of diagnosis across duplicates, I could use <code>slice()</code> and retain the first record. On the other hand, if I only needed to choose based on the diagnosis - I could use <code>filter()</code>. I can't figure out how to apply both conditions.</p>
  <p>Any help would be great! This was previously done manually in Excel, so figuring this out in R would be a huge step up.</p>