[R] getting variable names into formulas

J.R. Lockwood lockwood at rand.org
Mon Sep 9 18:46:17 CEST 2002


Hello,

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

"update(mod.baseline,. ~ . + dat[,which(names(dat)==thisvar)])"

but is not elegant and of only limited use because of the lack of
association with the variable name.

My question is whether there is a better way to do this, either with
"vars" as defined above or through some more clever definition.

Thanks to all for your consideration.

J.R. Lockwood
412-683-2300 x4941
lockwood at rand.org
http://www.rand.org/methodology/stat/members/lockwood/




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