title: Pairwise comparisons across columns from two different data frames
body: <p>Here is a toy dataframe, mydf</p>
  
  <pre><code>    M1  M2  M3  M4  M5  M6  M7  M8                      
  A1  2   2   1   1   1   1   0   2
  A2  0   1   2   2   1   1   0   2
  A3  2   0   0   1   1   0   1   1
  A4  2   0   1   2   2   1   1   0
  A5  1   2   0   2   2   1   0   1
  A6  2   1   1   2   2   1   1   1
  </code></pre>
  
  <p>And here is a screenshot of second dataframe, mismatches:</p>
  
  <pre><code>        M1      M2      M3      M4      M5  M6  M7      M8                              
  A1-A2   TRUE    NA      NA      NA      NA  NA  FALSE   FALSE
  A1-A3   FALSE   TRUE    NA      NA      NA  NA  NA      NA
  A1-A4   FALSE   TRUE    NA      NA      NA  NA  NA      TRUE
  A1-A5   NA      FALSE   NA      NA      NA  NA  FALSE   NA
  A1-A6   FALSE   NA      NA      NA      NA  NA  NA      NA
  A2-A1   TRUE    NA      NA      NA      NA  NA  FALSE   FALSE
  A2-A3   TRUE    NA      TRUE    NA      NA  NA  NA      NA
  A2-A4   TRUE    NA      NA      FALSE   NA  NA  NA      TRUE
  A2-A5   NA      NA      TRUE    FALSE   NA  NA  FALSE   NA
  A2-A6   TRUE    NA      NA      FALSE   NA  NA  NA      NA
  </code></pre>
  
  <p>Now I'd like to define a function to compare the two dataframes where mismatches[a,] == TRUE &amp; mydf[b,] != 1). So for the trio A1-A2/A3, the value for column M1 should be TRUE since mismatches for A1-A2 == TRUE &amp; mydf for A3 !=1. Here is a snapshot of my desired result</p>
  
  <pre><code>            M1      M2      M3  M4  M5  M6  M7      M8
  A1-A2/A3    TRUE    NA      NA  NA  NA  NA  FALSE   FALSE
  A1-A2/A4    TRUE    NA      NA  NA  NA  NA  FALSE   FALSE
  A1-A2/A5    FALSE   NA      NA  NA  NA  NA  FALSE   FALSE
  A1-A2/A6    TRUE    NA      NA  NA  NA  NA  FALSE   FALSE
  A1-A3/A2    FALSE   FALSE   NA  NA  NA  NA  NA      NA
  A1-A3/A4    FALSE   TRUE    NA  NA  NA  NA  NA      NA
  A1-A3/A5    FALSE   TRUE    NA  NA  NA  NA  NA      NA
  A1-A3/A6    FALSE   FALSE   NA  NA  NA  NA  NA      NA
  </code></pre>
  
  <p>I can't seem to find the question that I used as a model for my working code but here are some similar posts<a href="https://stackoverflow.com/questions/29045105/count-number-of-pairwise-differences-of-a-matrix-in-r">Count Number of Pairwise Differences...</a>;<a href="https://stackoverflow.com/questions/19933788/r-compare-all-the-columns-pairwise-in-matrix?rq=1">Compare all the columns pairwise...</a>;<a href="https://stackoverflow.com/questions/30766015/r-pairwise-comparison-of-matrix-columns-ignoring-empty-values?noredirect=1&amp;lq=1">Pairwise ... ignoring empty values</a>. Mismatches was derived from mydf by identifying 0/2 and 2/0 pairwise comparisons.</p>
  
  <pre><code>#make a 2/0 matrix for for calculating incompatibilities
  nohets &lt;- mydf
  nohets[nohets == 1] &lt;- NA
  #the incompatibility function
  incompatible &lt;- function(a,b,x)
      x[a,]!=x[b,]
  </code></pre>
  
  <p>The idea was to use this same strategy to compare mydf and mismatches.</p>
  
  <pre><code>#make a trio function
  trio_id &lt;- function(a,b,x,y)
      x[a,] == TRUE &amp; y[b,] !=1
  rows &lt;- c(row.names(mismatches),row.names(mydf))
  grid &lt;- subset(expand.grid(Var1=rows,Var2=rows))
  impossible &lt;- t(mapply(trio_id,grid$Var2,grid$Var1,
      MoreArgs=list(x=mismatches,y=mydf)))
  dimnames(impossible) &lt;- list(paste(grid$Var2,grid$Var1,
      sep="/"),names(mydf))
  </code></pre>
  
  <p>This runs, but does not give the desired result but rather</p>
  
  <pre><code>            M1      M2  M3  M4  M5  M6  M7      M8
  A1-A2/A3    NA      NA  NA  NA  NA  NA  FALSE   FALSE
  A1-A2/A4    NA      NA  NA  NA  NA  NA  FALSE   FALSE
  A1-A2/A5    NA      NA  NA  NA  NA  NA  FALSE   FALSE
  A1-A2/A6    NA      NA  NA  NA  NA  NA  FALSE   FALSE
  A1-A3/A2    FALSE   NA  NA  NA  NA  NA  NA      NA
  A1-A3/A4    FALSE   NA  NA  NA  NA  NA  NA      NA
  A1-A3/A5    FALSE   NA  NA  NA  NA  NA  NA      NA
  A1-A3/A6    FALSE   NA  NA  NA  NA  NA  NA      NA
  </code></pre>
  
  <p>I don't need every comparison calculated... and I suspect writing the code for every possible comparison in 'grid' is the source of my problem.</p>