[R] assign value to multiple objects with a given ls pattern

Greg Snow Greg.Snow at imail.org
Mon Feb 21 18:18:36 CET 2011


If instead of having a1, a2, etc. as global variables you put them into a list then this becomes simple.

The general rule is that if you ever want to do the same (or similar) think to a set of variable, then they should not have been separate variables, but part of a bigger one.  Lists work well for this (there are other possibilities as well, but lists cover most of the cases).

-- 
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 Nuno Prista
> Sent: Monday, February 21, 2011 9:22 AM
> To: r-help at R-project.org
> Subject: [R] assign value to multiple objects with a given ls pattern
> 
> Dear R colleagues,
> 
> This seems pretty straight forward but I have been banging my head on
> this for some time and can't seem to find a solution
> 
> suppose I have something like
> 
> a1<-1; a2<-2; a3<-3; a4<-4; b1<-3; b2<-4
> I would like to quickly assign to objects with a certain pattern, e.g.,
> those in
> 
> ls(pattern="a")
> 
> a specific value, e.g., "99", without having to assign each object at a
> time.
> 
> is there a way I can do this within a "for" cycle? I have tested
> several eval and parse statements but with no success.
> 
> Regards,
> 
> Nuno Prista
> 
> ____________________________
> PhD student
> Instituto de Oceanografia
> Faculdade de Ciências da Universidade de Lisboa
> Campo Grande, Lisboa,
> 1749-016 Lisboa,
> Portugal
> 
> ______________________________________________
> 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