title: Remove duplicate rows conditionally within group_by in dplyr
body: <p>I know that there is a lot on removing duplicates out there but my problem seems different.</p>
  
  <hr>
  
  <p>I have a <code>data.frame</code> similar to this:</p>
  
  
  
  <pre class="lang-r prettyprint-override"><code>library(dplyr)
  #&gt; 
  #&gt; Attaching package: 'dplyr'
  #&gt; The following objects are masked from 'package:stats':
  #&gt; 
  #&gt;     filter, lag
  #&gt; The following objects are masked from 'package:base':
  #&gt; 
  #&gt;     intersect, setdiff, setequal, union
  x &lt;- data.frame(id = c(1, 1, 1, 1, 2, 3, 3),
                  date = as.Date(c("2016-04-24", "2016-04-24", "2016-04-24",
                                   "2016-04-24", "2016-04-24", "2016-04-28",
                                   "2016-04-28")),
                  code = c("a", "b", "b", "a", "a", "a", "a"))
  x
  #&gt;   id       date code
  #&gt; 1  1 2016-04-24    a
  #&gt; 2  1 2016-04-24    b
  #&gt; 3  1 2016-04-24    b
  #&gt; 4  1 2016-04-24    a
  #&gt; 5  2 2016-04-24    a
  #&gt; 6  3 2016-04-28    a
  #&gt; 7  3 2016-04-28    a
  </code></pre>
  
  <p>I would like filter out all duplicates for <code>code</code> "a" but not "b". The expected output should look like this:</p>
  
  <pre class="lang-r prettyprint-override"><code>x[c(1:3, 5:6), ]
  #&gt;   id       date code
  #&gt; 1  1 2016-04-24    a
  #&gt; 2  1 2016-04-24    b
  #&gt; 3  1 2016-04-24    b
  #&gt; 5  2 2016-04-24    a
  #&gt; 6  3 2016-04-28    a
  </code></pre>
  
  <hr>
  
  <p>I had a similar question here: <a href="https://stackoverflow.com/questions/50738942/ignore-value-conditionally-within-group-by-in-dplyr/50739095?noredirect=1#comment88489653_50739095">Ignore value conditionally within group_by in dplyr</a> on which I base my following attempts. But these don't work and this is driving me nuts.</p>
  
  
  
  <pre class="lang-r prettyprint-override"><code>x %&gt;% group_by(id, date) %&gt;% 
    filter(!(code == "a" &amp; duplicated(code) == "a"))
  #&gt; # A tibble: 7 x 3
  #&gt; # Groups:   id, date [3]
  #&gt;      id date       code 
  #&gt;   &lt;dbl&gt; &lt;date&gt;     &lt;fct&gt;
  #&gt; 1    1. 2016-04-24 a    
  #&gt; 2    1. 2016-04-24 b    
  #&gt; 3    1. 2016-04-24 b    
  #&gt; 4    1. 2016-04-24 a    
  #&gt; 5    2. 2016-04-24 a    
  #&gt; 6    3. 2016-04-28 a    
  #&gt; 7    3. 2016-04-28 a
  
  
  x %&gt;% group_by(id, date) %&gt;% 
    filter(!(duplicated(code) == "a" &amp; "a" %in% code))
  #&gt; # A tibble: 7 x 3
  #&gt; # Groups:   id, date [3]
  #&gt;      id date       code 
  #&gt;   &lt;dbl&gt; &lt;date&gt;     &lt;fct&gt;
  #&gt; 1    1. 2016-04-24 a    
  #&gt; 2    1. 2016-04-24 b    
  #&gt; 3    1. 2016-04-24 b    
  #&gt; 4    1. 2016-04-24 a    
  #&gt; 5    2. 2016-04-24 a    
  #&gt; 6    3. 2016-04-28 a    
  #&gt; 7    3. 2016-04-28 a
  </code></pre>
  
  <p>Created on 2018-08-17 by the <a href="http://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.2.0).</p>
  
  <p>I guess the problem is with the <code>duplicated()</code> call that is not returning <code>TRUE</code> or <code>FALSE</code> but I am not sure.</p>