[R] use variable value to create new variable name

Peter Alspach Peter.Alspach at plantandfood.co.nz
Mon Jun 13 22:47:33 CEST 2011


Tena koe Maciek

It is possible; e.g.:

i <- 3
assign(paste('myVar', i, sep=''), 1:5)
myVar3
[1] 1 2 3 4 5

Personally, I find it is more convenient to use a list so that instead of myVar1, myVar2 etc I have a list object, myVar, with each element equivalent to myVar1, myVar2 etc.

HTH ...

Peter Alspach

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of maciek kryza
> Sent: Tuesday, 14 June 2011 2:14 a.m.
> To: r-help at r-project.org
> Subject: [R] use variable value to create new variable name
> 
> Hi,
> Is it possible (and how) to use a variable value to create a name of a
> new R
> variable? I want to do something like this:
> 
> for (i in 1:3) {
> newvarialbe_#i# = somedata
> }
> 
> where #i# is a value stored by i. In the first loop, there will be
> newvariable_1, in second newvariable_2 and so on.
> Thanks in advance,
> Maciek
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

The contents of this e-mail are confidential and may be subject to legal privilege.
 If you are not the intended recipient you must not use, disseminate, distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received this
 e-mail in error, please notify the sender and delete all material pertaining to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.



More information about the R-help mailing list