Skip to content
Prev 248816 / 398503 Next

environment question: changing variables from a formula through model.frame?

Hi Tal
On 29/01/11 13.25, Tal Galili wrote:
As far as I can tell, nothing unexpected happens.
I am not really sure what you are trying to achieve. In your
code you end up evaluating temp_y <- temp_y inside your function,
and then print out the model.frame of the formula -- again. You
did not modify anything in the model frame or the environment used
to create the model frame. The model frame is a data frame, which in
this case is created from the environment associated with the formula.
It doesn't?
It changes the temp_y in the global environment. But as far as I can tell,
either you do that our you change the variable in the environment of the
function, which will not affect the result of a later call to model.frame.

Alternatively, you might consider

assign(names(model.frame(formu))[1], temp_y, environment(formu))

which explicitly assigns the new value to the variable in the
environment associated with the formula. The difference is shown
if you try

foo3 <- function() {
   temp_x <- rep(1,5)
   temp_y <- c(1:5)	
   foo2(temp_y ~ temp_x)
}

foo3()

If your objective is to change data associated with the formula
locally in a function before passing the formula on to some other
function, you might consider passing on the modified model frame
instead of modifying the data in the environment associated with the
formula.

Hope it helped, Niels