Re: [R] for-loop with multiple variables changing
Sean Davis <[EMAIL PROTECTED]> writes: > On 2/6/06 5:53 AM, "Piet van Remortel" <[EMAIL PROTECTED]> 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: (Genstat has (had?) a "parallel for" structure, I believe.) > for (j in a) { > print(a[j]) > print(b[j]) > } I think you mean "for (i in 1:3) {...}". Also, check out the mapply() construct for related functionality. > 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 -- O__ Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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
Re: [R] for-loop with multiple variables changing
Hi do you want this? for(i in a) {print(a[i]); print(b[i])} HTH Petr On 6 Feb 2006 at 11:53, Piet van Remortel wrote: To: r-help@stat.math.ethz.ch From: Piet van Remortel <[EMAIL PROTECTED]> Date sent: Mon, 6 Feb 2006 11:53:33 +0100 Subject: [R] for-loop with multiple variables changing > 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@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 Petr Pikal [EMAIL PROTECTED] __ R-help@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
Re: [R] for-loop with multiple variables changing
On 2/6/06 6:23 AM, "Sean Davis" <[EMAIL PROTECTED]> wrote: > > > > On 2/6/06 5:53 AM, "Piet van Remortel" <[EMAIL PROTECTED]> 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@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 __ R-help@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
Re: [R] for-loop with multiple variables changing
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@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 > __ R-help@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
Re: [R] for-loop with multiple variables changing
On 2/6/06 5:53 AM, "Piet van Remortel" <[EMAIL PROTECTED]> 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) { 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@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
[R] for-loop with multiple variables changing
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@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