title: how to apply group data t.test to multi column in r?
body: <p>I plan to do t.test for <code>q1</code> by group <code>A&amp;B</code> in df</p>
  
  <pre><code>q1 q2 q3 group
  1  0  1  A
  0  1  0  B
  1  1  1  A
  0  1  0  B
  </code></pre>
  
  <p>Then the script is :</p>
  
  <pre><code>t.test(subset(df,group==A,select = c("q1")),subset(df,group==B,select = c("q1")),alternative = "two.sided")
  </code></pre>
  
  <p>I made a function for t.test script:</p>
  
  <pre><code>x&lt;-function(qnum){t.test(subset(df,group==A,select = c("qnum")),subset(df,group==B,select = c("qnum")),alternative = "two.sided")}
  </code></pre>
  
  <p>Then i think <code>apply</code> can give me t.test result for <code>q1,q2,q3...</code></p>
  
  <pre><code>y&lt;-select(df,grep("q\\d",colnames(df),perl=TRUE))
  apply(y,2,x)
  </code></pre>
  
  <p>but has error:</p>
  
  <pre><code>Error in `[.data.frame`(x, r, vars, drop = drop) :
  </code></pre>
  
  <p>how to automatic get the t.test result for multi columns?</p>