[R] using variable from for loop in naming new variables

Greg Snow Greg.Snow at imail.org
Thu Sep 16 21:47:53 CEST 2010


This is FAQ 7.21.

Note that the most important part of the FAQ is the last few lines where it essentially says "DON'T DO THIS".  If this is just a model for a bigger situation then definitely Don't Do This, as using assign and global variables will just create more and bigger future headaches.  Use another data structure like a list and all this becomes much easier.

-- 
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 Maas James Dr (MED)
> Sent: Thursday, September 16, 2010 9:45 AM
> To: r-help at r-project.org
> Subject: [R] using variable from for loop in naming new variables
> 
> Simple one here ... but can't get it to work ...
> 
> for (i in 1:4){
>     paste("stuff",[i]),sep="") <- 3 + i
> }
> 
> ls()
> rm(list=ls())
> 
> 
> 
> I just want it to create 4 new variables called stuff1, stuff2, stuff3,
> stuff4 with the corresponding assignments.  I realise that there are
> more elegant functions but this is just a model of a bigger situation.
> 
> Thanks
> 
> Jim
> 
> 
> ===============================
> Dr. Jim Maas
> University of East Anglia
> 
> 
> 	[[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.



More information about the R-help mailing list