title: Run a function inside data.table in R
body: <p>Hi I have some data in data.table format in R and I need to run some function.</p>
  
  <p>Let say I have a data.table called A with columns, "name" "height", "weight".</p>
  
  <p>I want to run some function, i.e. linear regression within data.table and store the coefficients, RMSE into the table results.</p>
  
  <pre><code>A[, .(beta = lm(height ~ weight)$coefficients[2], RMSE = 
       as.numeric(sqrt(crossprod(lm(height 
       ~ weight)$residuals)/(length(lm(height ~ weight)$residuals)- 
       (length(coef(lm(height ~ weight)))-1)))*100),
       by=.(name)]
  </code></pre>
  
  <p>My question: Is there a way to save the lm(height ~ weight) result as an object and then access this object's data so data.table don't need to run the lm function like 4 times in here?</p>
  
  <p>This runs but it is a bit too slow compared to me using foreach and loop over "name" as I have millions rows of data.</p>
  
  <p>Thanks.</p>