[R] looping variable names

Greg Snow Greg.Snow at imail.org
Wed Feb 2 23:34:44 CET 2011


The best way to do this is to not create those variables in the global workspace, but to create a list with the variables in the list (you can name the list elements and access them by name or by number).  With lists you can use lapply and sapply to do many of the things that you think you want to loop through names with.

-- 
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 hypermonkey22
> Sent: Wednesday, February 02, 2011 1:37 PM
> To: r-help at r-project.org
> Subject: [R] looping variable names
> 
> 
> Hi all,
> 
> I've been looking for a simple answer to the following problem.
> 
> Let's say that I can loop through, say, 100 values that need to be
> assigned
> to, say, the variables var1:var100.
> 
> Is there an elegant way to do this?
> 
> I have seen one or two similar questions...but they tend to be in more
> complicated contexts.
> Simple question, hopefully with a simple answer.
> 
> Thanks very much!
> --
> View this message in context: http://r.789695.n4.nabble.com/looping-
> variable-names-tp3255711p3255711.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.



More information about the R-help mailing list