title: R: t test over multiple columns using t.test function
body: <p>I tried to perform independent t-test for many columns of a dataframe. For example, i created a data frame</p>
  
  <pre><code>set seed(333)
  a &lt;- rnorm(20, 10, 1)
  b &lt;- rnorm(20, 15, 2)
  c &lt;- rnorm(20, 20, 3)
  grp &lt;- rep(c('m', 'y'),10)
  test_data &lt;- data.frame(a, b, c, grp)
  </code></pre>
  
  <p>To run the test, i used <code>with(df, t.test(y ~ group))</code> </p>
  
  <pre><code>with(test_data, t.test(a ~ grp))
  with(test_data, t.test(b ~ grp))
  with(test_data, t.test(c ~ grp))
  </code></pre>
  
  <p>I would like to have the outputs like this</p>
  
  <pre><code>mean in group m mean in group y  p-value
  9.747412        9.878820         0.6944
  15.12936        16.49533         0.07798 
  20.39531        20.20168         0.9027
  </code></pre>
  
  <p>I wonder how can I achieve the results using
  1. <code>for loop</code>
  2. <code>apply()</code>
  3. perhaps <code>dplyr</code></p>
  
  <p>This link <a href="https://stackoverflow.com/questions/9661469/r-t-test-over-all-columns">R: t-test over all columns</a> is related but it was 6 years old. Perhaps there are better ways to do the same thing. </p>