Hi, Say I want to do something like fitting 10 different sized trees with rpart() function. The only modification I need to do is to set 10 different cp's, which I have in a vector called foo. Can I do something like: for(i in 1:10) { rpart(y ~ ., cp = foo[i], data = mydata) } My problem is, I wish to save the 10 rpart objects into 10 different names, my.rpart1 ~ my.rpart10, for example. But I'm not sure how to do this... Cheers, Kevin ------------------------------------------------------------------------------ Ko-Kang Kevin Wang Postgraduate PGDipSci Student Department of Statistics University of Auckland New Zealand Homepage: http://www.stat.auckland.ac.nz/~kwan022 E-mail: kwan022 at stat.auckland.ac.nz -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Sat, 8 Jun 2002 22:44:07 +1200 (NZST) Ko-Kang Kevin Wang <kwan022 at stat.auckland.ac.nz> wrote:> Hi, > > Say I want to do something like fitting 10 different sized trees with > rpart() function. The only modification I need to do is to set 10 > different cp's, which I have in a vector called foo. > > Can I do something like: > > for(i in 1:10) { > rpart(y ~ ., cp = foo[i], data = mydata) > } > > My problem is, I wish to save the 10 rpart objects into 10 different > names, my.rpart1 ~ my.rpart10, for example. But I'm not sure how to do > this... >fits <- vector('list',10) names(fits) <- format(foo) # optional - will label list components for(i in 1:10) fits[[i]] <- rpart(...) Fetch the ith fit by using fits[[i]] (or fits[['formatted cp value']]). -Frank Harrell> > Cheers, > > Kevin > > ------------------------------------------------------------------------------ > Ko-Kang Kevin Wang > Postgraduate PGDipSci Student > Department of Statistics > University of Auckland > New Zealand > > Homepage: http://www.stat.auckland.ac.nz/~kwan022 > > E-mail: kwan022 at stat.auckland.ac.nz > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Frank E Harrell Jr Prof. of Biostatistics & Statistics Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences U. Virginia School of Medicine http://hesweb1.med.virginia.edu/biostat -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Kevin, At 10:44 PM 6/8/2002 +1200, Ko-Kang Kevin Wang wrote:>Say I want to do something like fitting 10 different sized trees with >rpart() function. The only modification I need to do is to set 10 >different cp's, which I have in a vector called foo. > >Can I do something like: > >for(i in 1:10) { > rpart(y ~ ., cp = foo[i], data = mydata) >} > >My problem is, I wish to save the 10 rpart objects into 10 different >names, my.rpart1 ~ my.rpart10, for example. But I'm not sure how to do >this...The first question is whether you really need different names. It's simpler to collect the results in a list; for example: result <- vector(mode='list', length=10) for(i in 1:10) result[[i]] <- rpart(y ~ ., cp = foo[i], data = mydata) Even more simply, using lapply: result <- lapply(1:10, function(i) rpart(y ~ ., cp = foo[i], data = mydata)) If you do need the results in separate variables, you could do something like the following: result <- lapply(1:10, function(i) eval(parse(text paste('my.rpart', i, ' <- rpart(y ~ ., cp = foo[', i, '], data = mydata)', sep="")))) (Obviously this is much more error-prone; make sure that I've got the quotes and commas in the right place.) I hope that this helps, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._