title: Understanding Vectorized Code In R
body: <p>I'm trying to understand the answer to this question using R and I'm struggling a lot.  </p>
  
  <p>The dataset for the R code can be found with this code</p>
  
  <pre><code>library(devtools)
  install_github("genomicsclass/GSE5859Subset")
  library(GSE5859Subset)
  data(GSE5859Subset) ##this loads the three tables you need
  </code></pre>
  
  <p>Here is the question</p>
  
  <p>Write a function that takes a vector of values e and a binary vector group coding two groups, and returns the p-value from a t-test: t.test( e[group==1], e[group==0])$p.value.</p>
  
  <p>Now define g to code cases (1) and controls (0) like this g &lt;- factor(sampleInfo$group)</p>
  
  <p>Next use the function apply to run a t-test for each row of geneExpression and obtain the p-value. What is smallest p-value among all these t-tests?</p>
  
  <p>The answer provided is </p>
  
  <pre><code>myttest &lt;- function(e,group){
      x &lt;- e[group==1]
      y &lt;- e[group==0]
      return( t.test(x,y)$p.value )
      }
  g &lt;- factor(sampleInfo$group)
  pvals &lt;- apply(geneExpression,1,myttest, group=g)
  min( pvals ) 
  </code></pre>
  
  <p>Which gives you the answer of 1.406803e-21. </p>
  
  <p>What exactly is the input of the "e" argument of the myttest function when you run this? Is it possible to write this function as a formula like </p>
  
  <pre><code>t.test(DV ~ sampleInfo$group)
  </code></pre>
  
  <p>The t test is comparing the gene expression values of the 24 people (the values of which I believe are in the "geneExpression" matrix) by what group they were 
  in which you can find in sampleInfo's "group" column. I've run t tests so many times in R, but for some reason I can't wrap my mind around what's going on in this code. </p>