title: Leave-one out means by group in R
body: <p>Imagine a table of individuals over time in different firms. I'm trying to compute for every individual the mean wage of their co-workers (ie the mean wage in their firm at time t excluding them). I have a working code using data.table in R which works, but I'm wondering whether there is a better, more efficient of doing this:</p>
  <pre><code>foo &lt;- data.table(
    i = rep(1:6, each = 2), 
    t = rep(1:2, 6),
    f = rep(1:2, each = 6),
    w = 1:12
  )
  
  foo[, x := mean(foo[t == .BY$t &amp; f == foo[i == .BY$i &amp; t == .BY$t]$f &amp; i != .BY$i]$w), by = .(i, t)]
  </code></pre>