[R] getting variable names into formulas

Thomas Lumley tlumley at u.washington.edu
Mon Sep 9 19:26:24 CEST 2002


On Mon, 9 Sep 2002, Uwe Ligges wrote:
> "J.R. Lockwood" wrote:
> >
> > I have a dataframe with several hundred variables.  I would like to
> > explore updates of some baseline lme fit by including each of some
> > subset of these variables, one at a time.  For various reasons it is
> > inconvenient to rely on the positions of the numbered columns in the
> > dataframe.  Here is what I want to do:
> >
> > mod.baseline<-lme(fixed=foo,data=dat,random=bar)
> > for(thisvar in vars){
> >   <collect stuff from update(mod.baseline, ~ . ~ + thisvar)>
> > }
> >
> > I have tried defining "vars" to be a vector of character strings
> > giving the names of the variables I want to use, and then using
> > "as.name(thisvar)" in the formula argument to update().  This doesn't
> > work (and might be embarrasingly naive).  What does seem to work is
<snip>
> You are looking for get().
>
> Uwe Ligges

No, he isn't.

Putting variable names into formulas is harder because of the nonstandard
evaluation rules for formulas.  This should work:

 mod.baseline<-lme(fixed=foo,data=dat,random=bar)
 for(thisvar in vars){
   ##<collect stuff from update(mod.baseline, ~ . ~ + thisvar)>
   expr<-substitute(update(mod.baseline, ~ . ~ + newvar),
				list(newvar=as.name(thisvar))
   newmodel<-eval(expr)
 }


	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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