Alexander.Herr at csiro.au
2007-Jul-04 00:01 UTC
[R] for loop doesn't stop with upper loop value
Hi list, could anyone please educate me on the following: lst<-seq(47, 239, by=12) for(n in lst) { lower=n; upper=lower+10 for(i in lower+2 : upper) { print(paste(n, " i: ", i, " lower: ",lower, " upper :", upper)) } } does not stop when i = upper A while loop fixes this but, I still don't understand why the for loop doesn't stop when I has the value of upper for(n in lst) { lower=n; upper=lower+10 while(lower !=upper +1) { print(paste(n, " lower: ",lower, " upper :", upper)) lower=lower+1 } } Any enlightment would be most welcome. Thankx Herry
_______________________________________________________________________________________ lower+2 : upper parses as lower + (2:upper). The colon operator has fairly high precedence. What you want is (lower + 2):upper -- Hong Ooi Senior Research Analyst, IAG Limited 388 George St, Sydney NSW 2000 +61 (2) 9292 1566 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Alexander.Herr at csiro.au Sent: Wednesday, 4 July 2007 10:01 AM To: r-help at stat.math.ethz.ch Subject: [R] for loop doesn't stop with upper loop value Hi list, could anyone please educate me on the following: lst<-seq(47, 239, by=12) for(n in lst) { lower=n; upper=lower+10 for(i in lower+2 : upper) { print(paste(n, " i: ", i, " lower: ",lower, " upper :", upper)) } } does not stop when i = upper A while loop fixes this but, I still don't understand why the for loop doesn't stop when I has the value of upper for(n in lst) { lower=n; upper=lower+10 while(lower !=upper +1) { print(paste(n, " lower: ",lower, " upper :", upper)) lower=lower+1 } } Any enlightment would be most welcome. Thankx Herry ______________________________________________ R-help at stat.math.ethz.ch mailing list stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. _______________________________________________________________________________________ The information transmitted in this message and its attachme...{{dropped}}
On 4/07/2007, at 12:01 PM, <Alexander.Herr at csiro.au> <Alexander.Herr at csiro.au> wrote:> Hi list, > > could anyone please educate me on the following: > > lst<-seq(47, 239, by=12) > > for(n in lst) > { > lower=n; upper=lower+10 > for(i in lower+2 : upper) > { > print(paste(n, " i: ", i, " lower: ",lower, " upper :", upper)) > } > } > > does not stop when i = upperTo see what's going on, type 239+2:249 Hints: o precedence of operators (this ain't APL) o parentheses are Good Things. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confidenti...{{dropped}}