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?