title: Run pairwise fisher's test on all column combinations between two data.frames
body: <p>I have two data.frames: editCounts and nonEditCounts. These structs are of the same dimensions and contain the same column and row names, but the actual data varies. Below are heads to each: </p>
  
  <pre><code>&gt; head(editCounts)
                          Samp0         Samp1       Samp2
  chr10_101992307             0             4           3
  chr10_101992684             4             0           1
  chr10_127480585             0             3           0
  chr10_16479385              3             3           3
  chr10_73979859              0             3           2
  chr10_73979940              0             3           8
  &gt; head(nonEditCounts)
                          Samp0         Samp1       Samp2
  chr10_101992307             0             4           3
  chr10_101992684            15             0           4
  chr10_127480585             0             6           0
  chr10_16479385              7             7           4
  chr10_73979859              0            13           7
  chr10_73979940              0            21          10
  </code></pre>
  
  <p>The ultimate goal here is to perform pair-wise fisher tests (using fisher.test()) on each column and row between each of the data.frames. As an output I'd would like to create a table contain the resulting p-values from each pair-wise comparison corresponding to each row name, e.g.:</p>
  
  <pre><code>               Samp0_vs_Samp1     Samp0_vs_Samp2     Samp1_vs_Samp2 
  chr10_101992307          pval               pval               pval 
  chr10_101992684          pval               pval               pval 
  chr10_127480585          pval               pval               pval 
  chr10_16479385           pval               pval               pval 
  chr10_73979859           pval               pval               pval 
  ...                       ...                ...                ...
  </code></pre>
  
  <p>So, take for example Samp0 and Samp1, the first fisher test would consist of a matrix looking something like this: </p>
  
  <pre><code>    &gt; tempMat=matrix(c(editCounts$ERR188028_GBR[1], nonEditCounts$ERR188028_GBR[1],
      +                  editCounts$ERR188035_GBR[1], nonEditCounts$ERR188035_GBR[1]), 2, 2)
      &gt; tempMat
           [,1] [,2]
      [1,]    0    4
      [2,]    0    4
  </code></pre>
  
  <p>These values correspond to the first row (chr10_101992307). In this case the fisher test would result in a p-value of 1.</p>
  
  <p>I know I can use combn() to compute each column permutation but I'm not exactly sure how to loop though each column, create a contingency table from the 4 values, and run fisher's test. The code I have wrote thus far is listed below; however, it throws an error when trying to create the the tempMat. </p>
  
  <pre><code>editCounts    &lt;- read.table("editCountMatrix.txt", sep="\t", header=TRUE, row.names=1)
  nonEditCounts &lt;- read.table("nonEditCountMatrix.txt", sep="\t", header=TRUE, row.names=1)
  
  pairwiseComb &lt;- combn(names(editCounts),2)
  
  for (j in seq(1,length(pairwiseComb),2)){
    tempCol1 = pairwiseComb[[j]]
    tempCol2 = pairwiseComb[[j+1]]
    cat("Processing: ",tempCol1," vs. ",tempCol2, "\n", sep="") # Prints correctly
    for (i in 1:nrow(editCounts)){
      tempMat=matrix(c(editCounts$tempCol1[i], nonEditCounts$tempCol1[i],
                   editCounts$tempCol2[i], nonEditCounts$tempCol2[i]), 2, 2)
      tempFisher=fisher.test(tempMat, alternative="two.sided")
      pval=tempFisher$p.value
      pvalAdj=p.adjust(pval,method="fdr")
    }
  }
  </code></pre>
  
  <p>The error this produces is shown below: </p>
  
  <pre><code>Error in matrix(c(editCounts$tempCol1[i], nonEditCounts$tempCol1[i], editCounts$tempCol2[i],  : 
    'data' must be of a vector type, was 'NULL'
  </code></pre>
  
  <p>Any help would be greatly appreciated. </p>
  
  <p>Thanks! </p>