title: data.table aggregations that return vectors, such as scale()
body: <p>I have recently been work with much larger datasets and have started learning and migrating to data.table to improve performance of aggregation/grouping. I have been unable to get certain expressions or functions to group as expected. Here is an example of a basic group by operation that I am having trouble with.</p>
  
  <pre><code>library(data.table)
  category &lt;- rep(1:10, 10)
  value &lt;- rnorm(100)
  df &lt;- data.frame(category, value)
  dt &lt;- data.table(df)
  </code></pre>
  
  <p>If I want to simply calculate the mean for each group by category. This works easily enough.</p>
  
  <pre><code>dt[,mean(value),by="category"]
  
      category          V1
   1:        1 -0.67555478
   2:        2 -0.50438413
   3:        3  0.29093723
   4:        4 -0.41684790
   5:        5  0.33921764
   6:        6  0.01970997
   7:        7 -0.23684245
   8:        8 -0.04280998
   9:        9  0.01838804
  10:       10  0.44295978
  </code></pre>
  
  <p>I run into problems if I try and use the scale function or even a simple expression subtracting the value from itself. The grouping is ignored and I get the function/expression applied to each row instead. The following returns all 100 rows instead of 10 group by categories.</p>
  
  <pre><code>dt[,scale(value),by="category"]
  
  
  dt[,value-mean(value),by="category"]
  </code></pre>
  
  <p>I thought recreating scale as function that returns a numeric vector instead of a matrix might help.</p>
  
  <pre><code>zScore &lt;- function(x) {
    z=(x-mean(x,na.rm=TRUE))/sd(x,na.rm = TRUE)
    return(z) 
  }
  
  dt[,zScore(value),by="category"]
  
    category          V1
    1:        1 -1.45114132
    2:        1 -0.35304528
    3:        1 -0.94075418
    4:        1  1.44454416
    5:        1  1.39448268
    6:        1  0.55366652
    ....
   97:       10 -0.43190602
   98:       10 -0.25409244
   99:       10  0.35496694
  100:       10  0.57323480
       category          V1
  </code></pre>
  
  <p>This also returns the zScore function applied to all rows (N=100) and ignoring the grouping. What am I missing in order to get scale() or a custom function to use the grouping like it did above when using mean()?</p>