title: using t.test within data.table on multiple columns
body: <p>I am trying to run a t.test() on multiple columns of data within 'j' in a data.table. I've found a way that works, but isn't very elegant, and I feel like there's probably a more concise way using .SDcols, but haven't had any luck looking through here, or the data.table vignette. If this has been asked previously I apologize and please point me in the right direction.</p>
  
  <p>My data.table has essentially the following format</p>
  
  <pre><code>DT &lt;- data.table(name = c('a', 'b', 'c', 'a', 'b', 'c'), 
                   y = c(rnorm(6, mean = 100, sd = 30)), 
                   z = c(rnorm(6, mean = 10, sd = 3)),
                   group = rep(c('One', 'Two'), 3))
  </code></pre>
  
  <p>When I want to run a t.test comparing the values of y in group One and group Two, it's very straightforward:</p>
  
  <pre><code>DT[,t.test(y~group)]
  </code></pre>
  
  <p>If I want to get an output for both y and z the following works, but is clunky and inelegant. And with my actual data, I'm trying to do this over many columns so it would be more time consuming to type out each iteration I would like to run.</p>
  
  <pre><code>DT[,.(t.test(y~group), t.test(z~group))]
  </code></pre>
  
  <p>In the data.table vignette, using a function over a specific subset of columns is achieved by </p>
  
  <pre><code>DT[,lapply(.SD, mean), .SDcols = c('y', 'z')]
  
  </code></pre>
  
  <p>However replacing mean with t.test yields a one sample t.test, while I'm trying to get a two sample t.test. I've tried:</p>
  
  <pre><code>DT[,lapply(.SD, t.test, formula = .SDcols ~ group, data = DT), .SDcols = c('y', 'z')]
  
  </code></pre>
  
  <p>But this gives me a comparison between y and z, not both the comparisons of y~group and z~group.</p>
  
  <p>I've tried several versions of lapply with a custom function to get the output I want, but I won't make anyone read through my walls of unsuccessful code. Needless to say I have been unable to get that to work.</p>
  
  <p><strong>Question:</strong>
  Is there a way via lapply() or function() or a way currently unknown to me, to get t.test to run over multiple columns of data within 'j' in a data.table?</p>
  
  <p>Thanks in advance for your help,
  Chris</p>