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) } So I assign the first value to rsq then cycle through models 2 through 5 gathering there values. The %i% in my third line indicates which object to draw from. The question is is there any way to pass a variable such as i as part of a name? Ken Kenneth B. Pierce Jr. Research Ecologist Landscape Ecology, Modeling, Mapping and Analysis Team PNW Research Station - USDA-FS 3200 SW Jefferson Way, Corvallis, OR 97331 ken.pierce@oregonstate.edu 541 750-7393 http://www.fsl.orst.edu/lemma/gnnfire [[alternative HTML version deleted]]
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 USDA-ARS PSWMRU University Park, PA 16802 Sarah.Goslee at ars.usda.gov
Ken, I have a similar example in my blog: http://statcompute.spaces.live.com/blog/cns!39C8032DBD1321B7!229.entry 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) > } > > > So I assign the first value to rsq then cycle through models 2 through 5 > gathering there values. The %i% in my third line indicates which object > to draw from. The question is is there any way to pass a variable such > as i as part of a name? > > Ken > > > > Kenneth B. Pierce Jr. > > Research Ecologist > > Landscape Ecology, Modeling, Mapping and Analysis Team > > PNW Research Station - USDA-FS > > 3200 SW Jefferson Way, Corvallis, OR 97331 > > ken.pierce at oregonstate.edu > > 541 750-7393 > > http://www.fsl.orst.edu/lemma/gnnfire > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- WenSui Liu (http://spaces.msn.com/statcompute/blog) Senior Decision Support Analyst Health Policy and Clinical Effectiveness Cincinnati Children Hospital Medical Center
you need something like the following, fit.lis <- list(model1, model2, model3, model4, model5) # or if you have many models fit.lis <- lapply(paste("model", 1:5, sep = ""), get) sapply(fit.lis, function(x) summary(x)$r.squared) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting "Pierce, Ken" <ken.pierce at oregonstate.edu>:> 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) > } > > > So I assign the first value to rsq then cycle through models 2 through 5 > gathering there values. The %i% in my third line indicates which object > to draw from. The question is is there any way to pass a variable such > as i as part of a name? > > Ken > > > > Kenneth B. Pierce Jr. > > Research Ecologist > > Landscape Ecology, Modeling, Mapping and Analysis Team > > PNW Research Station - USDA-FS > > 3200 SW Jefferson Way, Corvallis, OR 97331 > > ken.pierce at oregonstate.edu > > 541 750-7393 > > http://www.fsl.orst.edu/lemma/gnnfire > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
The trick is to use the function get():> vns <- paste("model",1:4,sep="") > x <- rnorm(10) > model1 <- lm(rnorm(10) ~ x) > model2 <- lm(rnorm(10) ~ x) > model3 <- lm(rnorm(10) ~ x) > model4 <- lm(rnorm(10) ~ x)> r.sq <- summary(model1)$r.square > for (i in 2:4) r.sq <- c(r.sq,summary(get(vns[i]))$r.square)On 12/09/06, Pierce, Ken <ken.pierce@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) > } > > > So I assign the first value to rsq then cycle through models 2 through 5 > gathering there values. The %i% in my third line indicates which object > to draw from. The question is is there any way to pass a variable such > as i as part of a name? > > Ken > > > > Kenneth B. Pierce Jr. > > Research Ecologist > > Landscape Ecology, Modeling, Mapping and Analysis Team > > PNW Research Station - USDA-FS > > 3200 SW Jefferson Way, Corvallis, OR 97331 > > ken.pierce@oregonstate.edu > > 541 750-7393 > > http://www.fsl.orst.edu/lemma/gnnfire > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP [[alternative HTML version deleted]]