0

I have a data.table with columns y and x and I would like to assign the fitted values to one column in the data.table. So I tried the following:

q <- quote(predict(lm(y ~ x)))
dt[, predict(lm(y ~ x))]   # works
dt[, eval(q)]              # also works and gives the same result as the line above
dt[, fitted := predict(lm(y ~ x))]  # correctly creates a column with fitted values
dt[, fitted := eval(q)]             # breaks
Error in eval(expr, envir, enclos) : object 'y' not found

It seems like the behavior regarding quote and eval is inconsistent depending on whether a new column is created or not. Am I missing something?

  • 3
    summary: update to the latest version or use `eval(q, .SD)` – eddi Nov 26 '13 at 17:57
  • `q <- quote(predict(lm(y ~ x, data=dt)))` would also work, but I have no idea if that would result in additional copies. – Roland Nov 26 '13 at 18:06

0 Answers0