Working with R, this is a real WTF:
R> f_string <- 'Sepal.Length ~ Sepal.Width'
R> l <- with(iris, lm(as.formula(f_string))) # works fine
R> f_formula <- as.formula(f_string)
R> l <- with(iris, lm(f_formula))
Error in eval(expr, envir, enclos) : object 'Sepal.Length' not found
Why does as.formula have to be inside the lm()
call? I get it that this is a question about which environment things are evaluated in, because this works:
R> f_formula <- with(iris, as.formula(f_string))
R> lm(f_formula)
but I'm having real trouble wrapping my head around why one works and the other one doesn't.
Your failing example fails because you are creating the formula with the global environment:
and there's no
Sepal.Length
there. If you create the appropriate objects in the global environment you can make it work:But that is completely ignoring the
iris
data. Welcome to the world of annoying R behaviour.The other examples are all computing the formula object within the
iris
data frame as an environment. If you debuglm
and take a look at whatformula
is in one of your working cases:you'll see the environment is no longer the global one. If you want to see what's in that environment, get it from the formula's attributes and list: