Given the following function:
f <- function (x, y, data) {
linm <- lm(y ~ x, data)
summary(linm)$r.squared
}
The following call fails:
d <- data.frame(a = c(1, 2), b = c(3, 4), d = c(5, 6))
f('a', 'b', d)
Since lm
obviously searches for columns x
and y
rather than substituting the variable names with their contents). How can I rectify this?
I tried playing around with expressions and substitute
, to no avail. Unfortunately I don’t understand entirely how R treats these objects and in which contexts expressions are evaluated so as a consequence I’m flying blindly. Here’s what I’ve tried (not working):
exp <- substitute(expression(y ~ x), list(x = x, y = y))
linm <- lm(formula = exp, data = data)
The exp
object actually looks promising when inspecting; unfortunately I cannot convince lm
to swallow it.