title: Compute p-values across all columns of (possibly large) matrices in R
body: <p>is there are any more efficient/faster way to compare two matrices (column by columns) and to compute p-values using t-test for no difference in means (eventually switching to the chisq.test when necessary)?</p>
  
  <p>Here is my solution:</p>
  
  <pre><code>## generate fake data (e.g., from treatment and control data)
  z0 &lt;- matrix(rnorm(100),10,10)
  z1 &lt;- matrix(rnorm(100, mean=1.1, sd=2),10,10)
  
  ## function to compare columns (bloody for loop)
  compare.matrix &lt;- function(z0, z1){
    pval &lt;- numeric(ncol(z0)) ## initialize
  
    for(i in 1:ncol(z0)){ ## compare columns
      pval[i] &lt;- t.test(z1[, i], z0[, i])$p.value
  
      ## if var is categorical, switch test type
      if ( length(unique(z1[,i]))==2){
        index &lt;- c(rep(0, nrow(z0)), rep(1, nrow(z1)))
        xx &lt;- c(z0[,i], z1[,i])
        pval[i] &lt;- chisq.test(table(xx, index), simulate.p.value=TRUE)$p.value      
      }
    }
    return(pval)  
  }
  compare.matrix(z0, z1)
  </code></pre>