[R] variables in object names

Sarah Goslee sarah.goslee at gmail.com
Tue Sep 12 22:20:49 CEST 2006

Hi Ken,

Not quite the way you're thinking about it, but yes, there is,
and it is very useful. See ?get for more information, but here's
the basics:

for(i in 2:5) {
  thismodel <- get(paste("model", i, sep=""))
  rsq <- c(rsq, summary(thismodel)$r.squared)

Also see ?assign for the opposite effect.

On 9/12/06, Pierce, Ken <ken.pierce at oregonstate.edu> wrote:
> Is there any way to put an argument into an object name. For example,
> say I have 5 objects,  model1, model2, model3, model4 and model5.
> I would like to make a vector of the r.squares from each model by code
> such as this:
> rsq <- summary(model1)$r.squared
> for(i in 2:5){
> rsq <- c(rsq, summary(model%i%)$r.squared)
> }
Sarah Goslee
University Park, PA 16802
Sarah.Goslee at ars.usda.gov

More information about the R-help mailing list