title: apply a function over groups of columns
body: <p>How can I use <code>apply</code> or a related function to create a new data frame that contains the results of the row averages of each pair of columns in a very large data frame? </p>
  
  <p>I have an instrument that outputs <code>n</code> replicate measurements on a large number of samples, where each single measurement is a vector (all measurements are the same length vectors). I'd like to calculate the average (and other stats) on all replicate measurements of each sample. This means I need to group <code>n</code> consecutive columns together and do row-wise calculations.  </p>
  
  <p>For a simple example, with three replicate measurements on two samples, how can I end up with a data frame that has two columns (one per sample), one that is the average each row of the replicates in <code>dat$a</code>, <code>dat$b</code> and <code>dat$c</code> and one that is the average of each row for <code>dat$d</code>, <code>dat$e</code> and <code>dat$f</code>.</p>
  
  <p>Here's some example data</p>
  
  <pre><code>dat &lt;- data.frame( a = rnorm(16), b = rnorm(16), c = rnorm(16), d = rnorm(16), e = rnorm(16), f = rnorm(16))
  
              a          b            c          d           e          f
  1  -0.9089594 -0.8144765  0.872691548  0.4051094 -0.09705234 -1.5100709
  2   0.7993102  0.3243804  0.394560355  0.6646588  0.91033497  2.2504104
  3   0.2963102 -0.2911078 -0.243723116  1.0661698 -0.89747522 -0.8455833
  4  -0.4311512 -0.5997466 -0.545381175  0.3495578  0.38359390  0.4999425
  5  -0.4955802  1.8949285 -0.266580411  1.2773987 -0.79373386 -1.8664651
  6   1.0957793 -0.3326867 -1.116623982 -0.8584253  0.83704172  1.8368212
  7  -0.2529444  0.5792413 -0.001950741  0.2661068  1.17515099  0.4875377
  8   1.2560402  0.1354533  1.440160168 -2.1295397  2.05025701  1.0377283
  9   0.8123061  0.4453768  1.598246016  0.7146553 -1.09476532  0.0600665
  10  0.1084029 -0.4934862 -0.584671816 -0.8096653  1.54466019 -1.8117459
  11 -0.8152812  0.9494620  0.100909570  1.5944528  1.56724269  0.6839954
  12  0.3130357  2.6245864  1.750448404 -0.7494403  1.06055267  1.0358267
  13  1.1976817 -1.2110708  0.719397607 -0.2690107  0.83364274 -0.6895936
  14 -2.1860098 -0.8488031 -0.302743475 -0.7348443  0.34302096 -0.8024803
  15  0.2361756  0.6773727  1.279737692  0.8742478 -0.03064782 -0.4874172
  16 -1.5634527 -0.8276335  0.753090683  2.0394865  0.79006103  0.5704210
  </code></pre>
  
  <p>I'm after something like this</p>
  
  <pre><code>            X1          X2
  1  -0.28358147 -0.40067128
  2   0.50608365  1.27513471
  3  -0.07950691 -0.22562957
  4  -0.52542633  0.41103139
  5   0.37758930 -0.46093340
  6  -0.11784382  0.60514586
  7   0.10811540  0.64293184
  8   0.94388455  0.31948189
  9   0.95197629 -0.10668118
  10 -0.32325169 -0.35891702
  11  0.07836345  1.28189698
  12  1.56269017  0.44897971
  13  0.23533617 -0.04165384
  14 -1.11251880 -0.39810121
  15  0.73109533  0.11872758
  16 -0.54599850  1.13332286
  </code></pre>
  
  <p>which I did with this, but is obviously no good for my much larger data frame...</p>
  
  <pre><code>data.frame(cbind(
  apply(cbind(dat$a, dat$b, dat$c), 1, mean),
  apply(cbind(dat$d, dat$e, dat$f), 1, mean)
  ))
  </code></pre>
  
  <p>I've tried <code>apply</code> and loops and can't quite get it together. My actual data has some hundreds of columns. </p>