title: subset data only if observations of 2 variables match with each other in R
body: <p>I have data like so:</p>
  
  <pre><code>a &lt;- c("blue", "red", "green", "blue","cyan")
  b &lt;- c("red","red","green","blue", "orange")
  df &lt;- data.frame(a,b)
  df
  
        a      b
  1  blue    red
  2   red    red
  3 green  green
  4  blue   blue
  5  cyan orange
  </code></pre>
  
  <p>I am trying to subset the rows if blue and red match with each other and watch with themselves. </p>
  
  <p>I am trying following code but there are still some other colors that are matching one of these colours when I see check them using table function. </p>
  
  <pre><code> sub &lt;- df[df$a %in% c("blue", "red" &amp; df$b %in% c("blue","red"), ]
   table(sub$a, sub$b)
  </code></pre>
  
  <p>It is very tricky for me. How can I tell R to subset only if blue and red match with each other and watch with themselves? </p>
  
  <p>Desired output is: </p>
  
  <pre><code>      a      b
  1  blue    red
  2   red    red
  3  blue   blue
  </code></pre>
  
  <p>Ultimate goal of doing this is to create 2 by 2 contingency table by separating then from 5 by 5 contingency table. If there is another suggestion to do this, that would be highly appreciated.</p>
  
  <p>Thanks in advance!</p>
  
  <p>This is what I mean by I don't want. I just want to keep observations blue &amp; red observations. I don't want any observation of green, orange, cyan.</p>
  
  <pre><code>            Blue        Red            Green        Orange   Cyan
    Blue       28          39              32            3        1  
    Red        47         244             184           56        3
    Green      0           0               0            0         0
    Orange     0           0               0            0         0
    Cyan       0           0               0            0         0
  </code></pre>