[R] environment question: changing variables from a formula through model.frame?
Niels Richard Hansen
Niels.R.Hansen+lists at math.ku.dk
Sun Jan 30 10:35:02 CET 2011
Hi Tal
On 29/01/11 13.25, Tal Galili wrote:
> Hello all,
>
> I came across a behavior of R with environments that I'm not sure what is
> causing it.
> It involves changing variables that are found through using model.frame on a
> formula inside a function.
> I wonder if it's a "bug" or a "feature". And in either case, how it might
> be managed.
As far as I can tell, nothing unexpected happens.
>
>
> Here is a simple example:
>
> # let's say we have an x and y variables:
> temp_x<- rep(1,5)
> temp_y<- c(1:5)
> # we now create a function that will take a formula of y~x and will (try and
> *fail* to) change "y" (only) inside the environment of this function:
> foo1<- function(formu)
> {
> print("Before changing the 'y' variable in the formula")
> print(model.frame(formu))
> temp_y<- model.frame(formu)[,1] +10
>
> the_txt<- paste(names(model.frame(formu))[1], "temp_y", sep = "<-")
> eval(parse(text = the_txt)) # jutter out y var so to be able to handle
> identical values.
>
> print("After changing the 'y' variable in the formula")
> print(model.frame(formu))
> # why isn't it printing the new y I just created for the enviornment of
> this function?
>
> }
> # running the function shows the problem:
> foo1(temp_y ~ temp_x)
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.
>
> # If I'll try it using<<-, this will change the y in the global environment
> (something I'd rather avoid)
> # for example:
> foo2<- function(formu)
> {
> print("Before changing the 'y' variable in the formula")
> print(model.frame(formu))
> temp_y<- model.frame(formu)[,1] +10
>
> the_txt<- paste(names(model.frame(formu))[1], "temp_y", sep = "<<-")
> eval(parse(text = the_txt)) # jutter out y var so to be able to handle
> identical values.
>
> print("After changing the 'y' variable in the formula")
> print(model.frame(formu))
> # why isn't it printing the new y I just created for the enviornment of
> this function?
It doesn't?
>
> }
> foo2(temp_y ~ temp_x)
> temp_y
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
>
>
>
> -------------------------------------------
> I know this question is somewhat of an oddity, but I hope some of you might
> help me understand what is happening here.
>
> Best,
> Tal
>
>
>
>
>
>
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili at gmail.com | 972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
> ----------------------------------------------------------------------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Niels Richard Hansen Web: www.math.ku.dk/~richard
Associate Professor Email: Niels.R.Hansen at math.ku.dk
Department of Mathematical Sciences nielsrichardhansen at gmail.com
University of Copenhagen Skype: nielsrichardhansen.dk
Universitetsparken 5 Phone: +1 510 502 8161
2100 Copenhagen Ø
Denmark
More information about the R-help
mailing list