title: R data.table: use optimised mean() function in composite call?
body: <p>I want to compute in R <code>data.table</code> deviations from group means. To do this efficiently, I would want to use the optimised <code>mean</code> function in data.table, but haven't found a way to use it within a <em>composite</em> call (i.e. <code>x - mean(x)</code>)? </p>
  
  <p>What I mean is that I can use <code>x[, lapply(.SD, function(x) x - mean(x)), by=id]</code>, but I suspect that this approach does not use the <em>optimised</em> version of mean in data.table. Indeed, comparing the speed of:</p>
  
  <ol>
  <li><code>x[, lapply(.SD, mean), by=id]</code></li>
  <li><code>x[, lapply(.SD, function(x) mean(x)), by=id]</code></li>
  </ol>
  
  <p>It turns out that in some cases 1) is 10 times faster than 2)! So how could I use a call like in 1), but this time for a composite function like <code>x -mean(x)</code>? I did not succeed using anonymous call <code>{...}</code> within  <code>lapply</code>.</p>
  
  <p>Thanks!</p>
  
  <h3>Simulation showing how faster <code>mean</code> versus <code>function(x) mean(x)</code> is:</h3>
  
  <pre><code>library(data.table)
  
  T = 50 
  N = 20000
  set.seed(123)
  data_sim &lt;- data.table(A = rnorm(N * T),
                         B1 = sample(c(0,1), N * T, replace = TRUE),
                         B2 = rnorm(N * T),
                         individual = rep(1:N, each = T))
  
  library(microbenchmark)
  
  mean2 &lt;- function(x) mean(x)
  
  microbenchmark(sol1 = data_sim[, lapply(.SD, mean), by=individual],
                 sol2 = data_sim[, lapply(.SD, mean2), by=individual],
                 sol3 = data_sim[, lapply(.SD, function(x) mean(x)), by=individual],
                 dev_mean = data_sim[, lapply(.SD, function(x) x- mean(x)), by=individual],
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>|expr     |       min|      mean|       max| neval|
  |:--------|---------:|---------:|---------:|-----:|
  |sol1     |  17.67686|  18.68033|  21.04078|     5|
  |sol2     | 369.69595| 378.91943| 400.77024|     5|
  |sol3     | 149.57088| 154.76857| 159.93155|     5|
  |dev_mean | 218.44641| 286.00977| 404.06092|     5|
  </code></pre>