title: reducing the number of comparisons in pairwise.t.test
body: <p>This is a question stimulated by a previous one I asked: <a href="https://stackoverflow.com/questions/16142981/using-tapply-dapply-etc-for-t-tests">using tapply/dapply etc for t.tests</a></p>
  
  <p>I have a data frame from an interlab study as follows <a href="http://pastebin.com/AD57AYD1" rel="nofollow noreferrer">http://pastebin.com/AD57AYD1</a></p>
  
  <p>Essentially lab=Laboratory, mat=material, fab=strength, thick=thickness</p>
  
  <p>I want t.test data to compare each lab for each type of material.   I.e., for mat=v, I want to run a t.test to compare lab B against lab S.  Similarly for materials c, n and l.</p>
  
  <p>My previous question was about using plyr to allow me to run  t.tests for each of these combinations.  However, it was pointed out that there is the issue of multiple comparisons to consider.</p>
  
  <p>I have tried to use the paired.t.test function on my data, but it did too many comparisons (i.e., it did a t-test of lab B nitrile versus lab S vinyl - which is irrelevant.   I called it like this:</p>
  
  <pre><code>pairwise.t.test(interlab$fab,interaction(interlab$mat,interlab$lab),paired=FALSE, pool.sd=FALSE)
  </code></pre>
  
  <p>and it gave me</p>
  
  <pre><code>&gt; pairwise.t.test(interlab$fab,interaction(interlab$mat,interlab$lab),paired=FALSE, pool.sd=FALSE)
  
  Pairwise comparisons using t tests with non-pooled SD 
  
      data:  interlab$fab and interaction(interlab$mat, interlab$lab) 
  
      c.B     l.B     n.B     v.B     c.S     l.S     n.S    
      l.B 0.54484 -       -       -       -       -       -      
      n.B 3.8e-07 1.9e-06 -       -       -       -       -      
      v.B 0.93881 0.22393 3.6e-07 -       -       -       -      
      c.S 0.00576 0.93881 1.2e-05 0.00026 -       -       -      
      l.S 0.00067 0.48601 2.5e-05 4.6e-05 0.89883 -       -      
      n.S 4.3e-12 2.2e-10 0.92366 5.4e-12 6.7e-10 7.7e-10 -      
      v.S 0.93881 0.93881 1.9e-06 0.31885 0.01217 0.00169 1.3e-10
  
      P value adjustment method: holm 
  </code></pre>
  
  <p>I am concerned that the adjusted p-values in this are wrong, because we were not comparing material n with l, or l with c - we are always lookiung at the same material when tested in both labs (i.e., material 'l' in lab "B" and "S").</p>
  
  <p>Is there any way to subset/group the data so that the appropriate call to 
  pairwise.t.test gives me the following comparisons only ?</p>
  
  <pre><code>        c.B     l.B     n.B     v.B     c.S     l.S     n.S    
      l.B -       -       -       -       -       -       -      
      n.B -       -       -       -       -       -       -      
      v.B -       -       -       -       -       -       -      
      c.S 0.00576 -       -       -       -       -       -      
      l.S -       0.48601 -       -       -       -       -      
      n.S -       -       0.92366 -       -       -       -      
      v.S -       -       -       0.31885 -       -       -      
  </code></pre>
  
  <p>Regards
  Pete</p>
  
  <p>EDIT: after comments from @John</p>
  
  <p>Whilst it doesn't seem possible to use the pairwise.t.test function in that manner, the previous solution from @droopy can be utilised in a call to the p.adjust function:</p>
  
  <pre><code> 
  &gt; FUN&lt;- function(x) {
     t.test(x[,"fab"] ~ x[,"lab"])$p.value
  }
  
  res&lt;-ddply(interlab, .(mat), FUN)
  res$adjpvalue&lt;-p.adjust(res$V1)
  
  res
  
  mat           V1   adjpvalue
  1   c 0.0004798071 0.001919228
  2   l 0.0607510365 0.121502073
  3   n 0.1847312857 0.184731286
  4   v 0.0354274420 0.106282326
  </code></pre>
  
  <p>Thanks to @John and @droopy for their help in this. </p>