[R] for loop doesn't stop with upper loop value

Hong Ooi Hong.Ooi at iag.com.au
Wed Jul 4 02:21:03 CEST 2007


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))

Any enlightment would be most welcome.


R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.


The information transmitted in this message and its attachme...{{dropped}}

More information about the R-help mailing list