[R] how to set the variable name in a loop

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Aug 4 16:34:57 CEST 2003


Simpler (and easier to read and avoids a conversion of i to character and 
back) is

for (i in 1:10) assign(paste("var", i, sep=""), i)

Neither of you needed the braces.

On Mon, 4 Aug 2003, Pfaff, Bernhard wrote:

> 
> > Hello,
> > 
> > I would like to have variables whose name are var1, var2, ... 
> > in a loop :
> > 
> > for (i in 1:10)
> > {
> >     var(i) <- i # where var(i) is var1, ....
> > }
> 
> how about:
> 
> for (i in 1:10){
>   eval(parse(text=paste("var", i, "<-", i, sep="")))
> }
> 
> HTH,
> Bernhard
> 
> 
> 
> 
> > 
> > Thanks in advance
> > 
> > Philippe
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > 
> 
> 
> ----------------------------------------------------------------------
> If you have received this e-mail in error or wish to read our e-mail 
> disclaimer statement and monitoring policy, please refer to 
> http://www.drkw.com/disc/email/ or contact the sender.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list