[R] for-loop with multiple variables changing

Sean Davis sdavis2 at mail.nih.gov
Mon Feb 6 12:37:08 CET 2006




On 2/6/06 6:23 AM, "Sean Davis" <sdavis2 at mail.nih.gov> wrote:

> 
> 
> 
> On 2/6/06 5:53 AM, "Piet van Remortel" <piet.vanremortel at gmail.com> wrote:
> 
>> Hi all,
>> 
>> Never really managed to build a for-loop with multiple running
>> variables in an elegant way.
>> 
>> Can anybody hint ?
>> 
>> See below for an example of what I would like.
>> 
>> EXAMPLE
>> a<-c(1,2,3)
>> b<-c("name1","name2","name3")
>> 
>> for( number in a, name in b ) {
>> print( number ) ##take a value
>> print( name ) ##and have its name available from a second list
>> }
>> 
>> Does R support this natively ?
> 
> I'm not sure what language does support this construct natively?  In any
> case, what about:
> 
>  for (j in a) {

OOPs.  Should be:
 for(j in 1:length(a)) {

(thanks, Adai--too early in the morning here)

>     print(a[j])
>     print(b[j])
>  }
> 
> You may be thinking of a "hash" structure.  If you are, you could look at
> using lists.  See the R-intro on using list data structures.
> 
> Sean
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list