title: R - aggregated data.table columns differently
body: <p>I am given a large data-table that needs to be aggregated according to the first column:</p>
  
  <p>The problem is the following:</p>
  
  <ol>
  <li>For several columns, one just has to form the sum for each category (given in column 1)</li>
  <li>For other columns, one has to calculate the mean</li>
  <li>There is a 1-1 correspondence between the entries in the first and second columns. Such that the entries of the second column should be kept.</li>
  </ol>
  
  <p>The following is a possible example of such a data-table. Let's assume that columns 3-9 need to be summed up and columns 10-12 need to be averaged.</p>
  
  <pre><code>library(data.table)
  set.seed(1)
  a&lt;-matrix(c("cat1","text1","cat2","text2","cat3","text3"),nrow=3,byrow=TRUE)
  M&lt;-do.call(rbind, replicate(1000, a, simplify=FALSE)) # where m is your matrix
  M&lt;-cbind(M,matrix(sample(c(1:100),3000*10,replace=TRUE ),ncol=10))
  M &lt;- as.data.table(M)
  </code></pre>
  
  <p>The result should be a table of the form</p>
  
  <pre><code>     V1    V2 V3 V4 V5  V6 V7 V8 V9 V10 V11 V12
  1: cat1 text1 27 81 78  95 27 22 12  76  18  76
  2: cat2 text2 38 48 70 100 11 97  8  53  56  33
  3: cat3 text3 58 18 66  24 14 73 18  27  92  70
  </code></pre>
  
  <p>but with entries the corresponding sums respective averages.</p>