title: How to keep columns in the output when grouping a data.table by several columns
body: <p>I am trying to "tidy" a large dataset, where multiple different types of data is merged in columns, and some data in column names. This is <a href="http://varianceexplained.org/r/tidy-genomics/" rel="nofollow noreferrer">a common scenario in biological dataset</a>. </p>
  
  <p>My data table has replicate measurements which I want to collapse into a mean. Converting the data into tidy format, these replicate values become additional rows. If I try to aggregate/group by several columns and calculate the mean of the replicates:</p>
  
  <pre><code>collapsed.data &lt;- tidy.dt[, mean(expression, na.rm = T), by=list(Sequence.window,Gene.names,ratio,enrichment.type,condition)]
  </code></pre>
  
  <p>I get a resultant table that has only the columns used in <code>by</code> statement and followed with the <code>mean(expression)</code> as column <code>V1</code>. Is it possible to get all the other (unchanged) columns as well?</p>
  
  <p>A minimalist example showing what I am trying to achieve is as follows:</p>
  
  <pre><code>library(data.table)
  dt &lt;- data.table(a = c("a", "a", "b", "b", "c", "a", "c", "a"), b = rnorm(8), 
                   c = c(1,1,1,1,1,2,1,2), d = rep('x', 8), e = rep('test', 8))
  dt[, mean(b), by = list(a, c)]
  #   a c         V1
  #1: a 1 -0.7597186
  #2: b 1 -0.3001626
  #3: c 1 -0.6893773
  #4: a 2 -0.1589146
  </code></pre>
  
  <p>As you can see the columns <code>d</code> and <code>e</code> are dropped. </p>