[R] taking variables from data.frame

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jan 4 14:10:15 CET 2002


On Fri, 4 Jan 2002, MUGGEO VITO wrote:

> Two of the arguments of my function fn(), say, are a glm-obj and a variable
> in the data.frame where there are the variable of the obj too.
> Is there a way to build a function that takes the variables form the same
> data.frame? For instance, the update() function does so:

No, model.frame called from the glm function does so, not update.  update
just creates a new call to glm, and calls it.

> >obj<-glm(....,data=mydata)
> >update(obj,.~.+x) #works becuse it searches x in mydata
>
> But for my function:
> >fn(....,obj, mydata$x) #it works
>
> and
> >fn(....,obj, x) #doesn't works: x not found!
>
> Of course I can attach the data.frame in the function by something like:
>
> fn<-function(....,data){
>  attach(data)
>  ....
>  detach()
>  }
>
> but is there anyone that can suggest me anything better?

Hint: try ?eval.  The critical line in model.frame.default is

    variables <- eval(attr(formula, "variables"), data, env)


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list