Hi guys, I want to create variable on the fly: for example for (i in 1:10) { cat(paste("VAR",i,sep="")) } Will print VAR1, VAR2 etc up to VAR10. However I want to make these into variables, and then give them a value, for example: vect = c(10:20) for (i in 1:10) { cat(paste("VAR",i,sep="")) = vect[i] } THis doesnt work but I hope it demonstrates what I'm trying to do: I'm trying to produce 10 variables, from VAR1 to VAR10, which have the numbers 11...20 respectively, so I would end up with VAR1 = 11 VAR2 = 12 ... VAR10 = 20 Hope this makes sense!! Does anyone have any ideas? -- View this message in context: http://www.nabble.com/produce-variable-on-the-fly-tp18943749p18943749.html Sent from the R help mailing list archive at Nabble.com.
On Tue, 12 Aug 2008, jimineep wrote:> > Hi guys, > > I want to create variable on the fly: for exampleSee ?assign HTH, Chuck> > for (i in 1:10) { > cat(paste("VAR",i,sep="")) > } > Will print VAR1, VAR2 etc up to VAR10. However I want to make these into > variables, and then give them a value, for example: > > vect = c(10:20) > > for (i in 1:10) { > cat(paste("VAR",i,sep="")) = vect[i] > } > > THis doesnt work but I hope it demonstrates what I'm trying to do: I'm > trying to produce 10 variables, from VAR1 to VAR10, which have the numbers > 11...20 respectively, so I would end up with > > VAR1 = 11 > VAR2 = 12 > ... > VAR10 = 20 > > Hope this makes sense!! Does anyone have any ideas? > -- > View this message in context: http://www.nabble.com/produce-variable-on-the-fly-tp18943749p18943749.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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
?assign , or consider a named vector/list. jimineep wrote:> Hi guys, > > I want to create variable on the fly: for example > > for (i in 1:10) { > cat(paste("VAR",i,sep="")) > } > Will print VAR1, VAR2 etc up to VAR10. However I want to make these into > variables, and then give them a value, for example: > > vect = c(10:20) > > for (i in 1:10) { > cat(paste("VAR",i,sep="")) = vect[i] > } > > THis doesnt work but I hope it demonstrates what I'm trying to do: I'm > trying to produce 10 variables, from VAR1 to VAR10, which have the numbers > 11...20 respectively, so I would end up with > > VAR1 = 11 > VAR2 = 12 > ... > VAR10 = 20 > > Hope this makes sense!! Does anyone have any ideas?
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible URL : <https://stat.ethz.ch/pipermail/r-help/attachments/20080812/baa3900a/attachment.pl>
jimineep <jamesrperkins <at> hotmail.com> writes:> > > Hi guys, > > I want to create variable on the fly: for example > > for (i in 1:10) { > cat(paste("VAR",i,sep="")) > } > Will print VAR1, VAR2 etc up to VAR10. However I want to make these into > variables, and then give them a value, for example: > > vect = c(10:20) > > for (i in 1:10) { > cat(paste("VAR",i,sep="")) = vect[i] > } >This is almost a candidate for a FAQ: see http://wiki.r-project.org/rwiki/doku.php?id=tips:data-misc:create_var_names for the answer ("assign") and some reasons why it might be better to use lists instead. Ben Bolker
On Tue, 12 Aug 2008, Ben Bolker wrote:> jimineep <jamesrperkins <at> hotmail.com> writes: > >> >> >> Hi guys, >> >> I want to create variable on the fly: for example >> >> for (i in 1:10) { >> cat(paste("VAR",i,sep="")) >> } >> Will print VAR1, VAR2 etc up to VAR10. However I want to make these into >> variables, and then give them a value, for example: >> >> vect = c(10:20) >> >> for (i in 1:10) { >> cat(paste("VAR",i,sep="")) = vect[i] >> } >> > > This is almost a candidate for a FAQ: see >It *is* FAQ 7.21. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle