title: Count unique strings that only occur in a single group based on all possible groups
body: <p>I have the following <code>df</code></p>
  <pre><code>a = data.frame(PA = c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;), Family = c(&quot;aa&quot;, &quot;ab&quot;, &quot;ac&quot;, &quot;aa&quot;, &quot;ad&quot;))
  </code></pre>
  <p>What I want to obtain is a count of unique 'Family' strings (aa, ab, ac, ad) in each PA (A or B) based on all possible PAs. For example, aa is a unique string for A and B, but since it occurs in both PAs I don't want it. On the other hand, ab and ac are unique for PA A and only occur in PA A: that's what I want.</p>
  <p>Using <code>dplyr</code> I was doing something like:</p>
  <pre><code>df &gt;%&gt; group_by(PA) %&gt;%
  summarise(count_family = n_distinct(Family))
  </code></pre>
  <p>But this only returns unique terms inside each PA — and I want unique Families that occur inside unique PAs based on all possible PAs</p>