[R] for-loop with multiple variables changing

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Mon Feb 6 12:31:20 CET 2006


If you want a one-to-one action between corresponding pairs of "a" and
"b", then how about simply :

 for( i in 1:length(a) ){
  print( number[i] )
  print( name[i] )
 }

If you want the first element of "a" to work with all elements of "b",
the second element of "a" to work with all elements of "b", ... then you
may find functions such as outer, sapply, mapply helpful.

Regards, Adai



On Mon, 2006-02-06 at 11:53 +0100, Piet van Remortel 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 ?
> 
> thanks !
> 
> Piet
> (Univ. of Antwerp - Belgium)
> 
> ______________________________________________
> 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