Hi all, I've been looking for a simple answer to the following problem. Let's say that I can loop through, say, 100 values that need to be assigned to, say, the variables var1:var100. Is there an elegant way to do this? I have seen one or two similar questions...but they tend to be in more complicated contexts. Simple question, hopefully with a simple answer. Thanks very much! -- View this message in context: http://r.789695.n4.nabble.com/looping-variable-names-tp3255711p3255711.html Sent from the R help mailing list archive at Nabble.com.
The best way to do this is to not create those variables in the global workspace, but to create a list with the variables in the list (you can name the list elements and access them by name or by number). With lists you can use lapply and sapply to do many of the things that you think you want to loop through names with. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of hypermonkey22 > Sent: Wednesday, February 02, 2011 1:37 PM > To: r-help at r-project.org > Subject: [R] looping variable names > > > Hi all, > > I've been looking for a simple answer to the following problem. > > Let's say that I can loop through, say, 100 values that need to be > assigned > to, say, the variables var1:var100. > > Is there an elegant way to do this? > > I have seen one or two similar questions...but they tend to be in more > complicated contexts. > Simple question, hopefully with a simple answer. > > Thanks very much! > -- > View this message in context: http://r.789695.n4.nabble.com/looping- > variable-names-tp3255711p3255711.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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.
Thanks for the reply! This sounds great. Is there a nice way to store these 100 variable names in a list without typing them all out? -- View this message in context: http://r.789695.n4.nabble.com/looping-variable-names-tp3255711p3256356.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 03.02.2011 00:56:40:> > Thanks for the reply! > > This sounds great. Is there a nice way to store these 100 variable namesin> a list without typing them all out?Why? When you need your 42nd variable from a list lll you can just use lll[42] and if you want each variable to have completely different name like foo, asdsdf, jgjgjgjg, ble, bla or other you need to do it manually. Or maybe use sample(letters, 6) to get random 6 letters names. Regards Petr> -- > View this message in context:http://r.789695.n4.nabble.com/looping-variable-> names-tp3255711p3256356.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
As Greg wrote, a list is in most circumstances a better way to store many objects. But you can use 'assign' and 'get' to create and access (global) variables #creation for (i in 1:100) assign(paste("var",i,sep=""),rnorm(5)) #access i-th variable i<-15 get(paste("var",i,sep="")) hth. Am 02.02.2011 21:36, schrieb hypermonkey22:> > Hi all, > > I've been looking for a simple answer to the following problem. > > Let's say that I can loop through, say, 100 values that need to be assigned > to, say, the variables var1:var100. > > Is there an elegant way to do this? > > I have seen one or two similar questions...but they tend to be in more > complicated contexts. > Simple question, hopefully with a simple answer. > > Thanks very much!-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790