2

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.

Konrad Rudolph
  • 506,650
  • 124
  • 909
  • 1,183
  • 4
    See `reformulate`: http://stackoverflow.com/questions/12967797/is-there-a-better-alternative-than-string-manipulation-to-programmatically-build/12967834#12967834 – flodel Feb 07 '13 at 18:26

1 Answers1

7

You may use the formula function. The following should work :

f <- function (x, y, data) {
    linm <- lm(formula(paste(y,"~",x)), data)
    summary(linm)$r.squared
}
juba
  • 45,570
  • 12
  • 106
  • 116