title: Conditional statement in dplyr/tidyverse function to exclude comparisons among same levels of a factor
body: <p>I have a dataframe like so: </p>
  
  <pre><code> data = read.table(text = "region     plot    species
   1          1A      A_B  
   1          1A      A_B
   1          1B      B_C
   1          1C      A_B
   1          1D      C_D
   2          2A      B_C
   2          2A      B_C
   2          2A      E_F
   2          2B      B_C
   2          2B      E_F     
   2          2C      E_F
   2          2D      B_C
   3          3A      A_B
   3          3B      A_B", stringsAsFactors = FALSE, header = TRUE)
  </code></pre>
  
  <p>I wanted to compare each level of <code>plot</code> to get a count of unique <code>species</code> matches among two plot comparisons. However, I do not want comparisons among the same plots (i.e. remove/do not include 1A_1A or 1B_1B or 2C_2C, ect.). The output for this example should appear as follows: </p>
  
  <pre><code>output&lt;-
    region  plot   freq
    1     1A_1B     0     
    1     1A_1C     1
    1     1A_1D     0
    1     1B_1C     0    
    1     1B_1D     0 
    1     1C_1D     0
    2     2A_2B     2     
    2     2A_2C     1
    2     2A_2D     1
    2     2B_2C     1    
    2     2B_2D     1 
    2     2C_2D     0
    3     3A_3B     1  
  </code></pre>
  
  <p>I have adapted the following code from @HubertL, <a href="https://stackoverflow.com/questions/44728704/convert-list-of-matrices-to-a-single-data-frame/44729050?noredirect=1#comment76595549_44729050">Convert list of matrices to a single data frame</a>
  but struggle to incorporate an appropriate if else statement to meet this condition: </p>
  
  <pre><code>library(tidyverse)
  
  data %&gt;% group_by(region, species) %&gt;% 
      filter(n() &gt; 1) %&gt;%
      summarize(y = list(combn(plot, 2, paste, collapse="_"))) %&gt;% 
      unnest %&gt;%
      group_by(region, y) %&gt;% 
      summarize(ifelse(plot[i] = plot[i], freq = 
      length(unique((species),)
  </code></pre>