Nicklas Pettersson
2008-Nov-18 17:58 UTC
[R] How do I generate multiple (similar) objects within R?
Hi, I wonder if anyone knows how to generate a list of objects, e.g. ten vectors with names: vect1, vect2, ... , vect10. My own idea was to use something like: for (i in 1:10) print(paste("vect", i,"<-NULL",sep="")) but the result is: "vect1<-NULL" ... "vect10<-NULL" and not vect1<-NULL ... vect10<-NULL as I would like. Does anyone know?
Uwe Ligges
2008-Nov-18 18:01 UTC
[R] How do I generate multiple (similar) objects within R?
An, again FAQ "How can I turn a string into a variable?" .... Uwe Ligges Nicklas Pettersson wrote:> Hi, > > I wonder if anyone knows how to generate a list of objects, e.g. ten > vectors with names: vect1, vect2, ... , vect10. > My own idea was to use something like: > > for (i in 1:10) > print(paste("vect", i,"<-NULL",sep="")) > > but the result is: > > "vect1<-NULL" > ... > "vect10<-NULL" > > and not > > vect1<-NULL > ... > vect10<-NULL > > as I would like. Does anyone know? > > ______________________________________________ > 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.
jim holtman
2008-Nov-18 18:03 UTC
[R] How do I generate multiple (similar) objects within R?
'assign' for (i in 1:10) assign(paste('vect', i, sep=''), NULL) or use a 'list' On Tue, Nov 18, 2008 at 12:58 PM, Nicklas Pettersson <Nicklas.Pettersson at stat.su.se> wrote:> Hi, > > I wonder if anyone knows how to generate a list of objects, e.g. ten vectors > with names: vect1, vect2, ... , vect10. > My own idea was to use something like: > > for (i in 1:10) > print(paste("vect", i,"<-NULL",sep="")) > > but the result is: > > "vect1<-NULL" > ... > "vect10<-NULL" > > and not > > vect1<-NULL > ... > vect10<-NULL > > as I would like. Does anyone know? > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Ben Bolker
2008-Nov-18 22:01 UTC
[R] How do I generate multiple (similar) objects within R?
Nicklas Pettersson <Nicklas.Pettersson <at> stat.su.se> writes:> > Hi, > > I wonder if anyone knows how to generate a list of objects, e.g. ten > vectors with names: vect1, vect2, ... , vect10. > My own idea was to use something like: > > for (i in 1:10) > print(paste("vect", i,"<-NULL",sep="")) >I think this is FAQ 7.21, "how can I turn a string into a variable": http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f cheers Ben Bolker
Dieter Menne
2008-Nov-20 16:11 UTC
[R] How do I generate multiple (similar) objects within R?
Nicklas Pettersson <Nicklas.Pettersson <at> stat.su.se> writes:> I wonder if anyone knows how to generate a list of objects, e.g. ten > vectors with names: vect1, vect2, ... , vect10. > My own idea was to use something like: > > for (i in 1:10) > print(paste("vect", i,"<-NULL",sep="")) >for (i in 1:10) eval(parse(text=paste("vect", i,"<-NULL",sep=""))) ls() But better use a list or vector for what you are trying to do. Note that the "list of objects" is not exactly what you intended with your example (which otherwise is nice, because it shows clearly what did not work). Dieter