title: R data.table with Box Cox transform from MASS package
body: <p>I'm trying to accomplish something fairly simple. I have a data table created as follows</p>
  
  <pre><code>z = data.table(id = c('a'),x = 1:4,y=c(48,80,44,34))
  z
  id x  y
  1:  a 1 48
  2:  a 2 80
  3:  a 3 44
  4:  a 4 34
  </code></pre>
  
  <p>Next, I've created a function that does the Box-Cox transform from the package MASS using the function <code>boxcox</code>. The function is shown below.</p>
  
  <pre><code>fn.lm = function(x,y){ 
   fit = lm(y~x)
   z = boxcox(fit)
   lambda = z$x[which.max(z$y)]
   return(lambda)
  }
  </code></pre>
  
  <p>Finally, I want to apply this function to all id's in the data table z as follows.</p>
  
  <pre><code>z[,list(slope = fn.lm(x,y)),by=id]
  </code></pre>
  
  <p>But when I run this last step I get the following error</p>
  
  <pre><code>Error in eval(expr, envir, enclos) : object 'y' not found
  </code></pre>
  
  <p>I suspect it has something to do with environments as it says it can't find <code>y</code>. But how do I fix this? Any help appreciated.
  Thanks in advance.</p>