In the windows version of R (1.3.0) is the following a bug, a known problem, or expected behavior: > for (i in 1:2) { + for (j in i+1:3) { + print(j) + } + } [1] 2 [1] 3 [1] 4 ???? [1] 3 [1] 4 ???? [1] 5 ???? > Conversely, the following behaves as expected: > for (i in 1:2) { + k <- i+1 + for (j in k:3) { + print(j) + } + } [1] 2 [1] 3 [1] 3 > This is under NT4, SP5 using a pre-compiled binary from CRAN. Thanks, -jh- ========================================John M. Heumann, Agilent Technologies 815 14th St. S.W., Loveland, CO 80537 USA Email: john_heumann at agilent.com Phone: 970 679-3761 FAX: 970 679-5399 ========================================-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 11 Sep 2001, HEUMANN,JOHN (A-Loveland,ex1) wrote:> In the windows version of R (1.3.0) is the following a bug, a > known problem, or expected behavior: > > > for (i in 1:2) { > + for (j in i+1:3) { > + print(j) > + } > + } > [1] 2 > [1] 3 > [1] 4 ???? > [1] 3 > [1] 4 ???? > [1] 5 ???? > >It's expected behaviour (if you expect it) Try i<-1 i+1:3 (i+1):3 to see what is going on -thomas Thomas Lumley Asst. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This is a "gotcha", which I think I will add to my list of "R traps" (http://www.zoo.ufl.edu/bolker/emd/R/R-traps.html). The problem is in "operator precedence", or "priority": the colon operator has higher precedence than the plus operator, so R treats i+1:3 as i+(1:3) rather than (i+1):3 (which is what you should write to get the behavior you expected). I'm not sure where this is found in the documentation. OK, p. 14 of the "Introduction to R" says The colon operator has highest priority within an expression, so, for example 2*1:15 is the vector c(2, 4, ..., 28, 30). Put n <- 10 and compare the sequences 1:n-1 and 1:(n-1). On Tue, 11 Sep 2001, HEUMANN,JOHN (A-Loveland,ex1) wrote:> In the windows version of R (1.3.0) is the following a bug, a > known problem, or expected behavior: > > > for (i in 1:2) { > + for (j in i+1:3) { > + print(j) > + } > + } > [1] 2 > [1] 3 > [1] 4 ???? > [1] 3 > [1] 4 ???? > [1] 5 ???? > > > > Conversely, the following behaves as expected: > > > for (i in 1:2) { > + k <- i+1 > + for (j in k:3) { > + print(j) > + } > + } > [1] 2 > [1] 3 > [1] 3 > > > > This is under NT4, SP5 using a pre-compiled binary from CRAN. > > Thanks, > -jh- > > ========================================> John M. Heumann, Agilent Technologies > 815 14th St. S.W., Loveland, CO 80537 USA > Email: john_heumann at agilent.com > Phone: 970 679-3761 FAX: 970 679-5399 > ========================================> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"HEUMANN,JOHN (A-Loveland,ex1)" wrote:> > In the windows version of R (1.3.0) is the following a bug, a > known problem, or expected behavior: > > > for (i in 1:2) { > + for (j in i+1:3) { > + print(j) > + } > + } > [1] 2 > [1] 3 > [1] 4 ???? > [1] 3 > [1] 4 ???? > [1] 5 ????Looks extremly as expected: # outer loop i=1 # [1] 2 i=1, j=i+1 # [1] 3 i=1, j=i+2 # [1] 4 i=1, j=i+3 # outer loop i=2 # [1] 3 i=2, j=i+1 # [1] 4 i=2, j=i+2 # [1] 5 i=2, j=i+3> Conversely, the following behaves as expected: > > > for (i in 1:2) { > + k <- i+1 > + for (j in k:3) { > + print(j) > + } > + } > [1] 2 > [1] 3 > [1] 3What you mean is (the braces!!!): for (i in 1:2) { for (j in (i+1):3) print(j) } Have a look at this (no spaces are here asn intentional negative example): 1+1:3 # the sequence is evaluated at first! Another way to do your calculation: i <- 1:2 j <- 3 lapply(i, function(x) seq(x+1,j)) Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 11 Sep 2001 12:54:49 -0600, you wrote in message <9153F74610B7D311B83900902771C8710A4CE336 at axcs07.cos.agilent.com>:>In the windows version of R (1.3.0) is the following a bug, a >known problem, or expected behavior: > > > for (i in 1:2) { > + for (j in i+1:3) { > + print(j) > + } > + } > [1] 2 > [1] 3 > [1] 4 ???? > [1] 3 > [1] 4 ???? > [1] 5 ????I think you're fooled by operator precedence. The expression "i+1:3" is equivalent to "i + (1:3)", i.e. c(i+1, i+2, i+3). Duncan Murdoch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
It seems the expected behavior to me, given that the : operator has higher precedence than +.> j <- 1 > j+1:3[1] 2 3 4> From: "HEUMANN,JOHN (A-Loveland,ex1)" <john_heumann at agilent.com> > Date: Tue, 11 Sep 2001 12:54:49 -0600 > Sender: owner-r-help at stat.math.ethz.ch > Precedence: SfS-bulk > Content-Type: text/plain; > charset="ISO-8859-1" > Content-Length: 1047 > > In the windows version of R (1.3.0) is the following a bug, a > known problem, or expected behavior: > > > for (i in 1:2) { > + for (j in i+1:3) { > + print(j) > + } > + } > [1] 2 > [1] 3 > [1] 4 ???? > [1] 3 > [1] 4 ???? > [1] 5 ???? > > > > Conversely, the following behaves as expected: > > > for (i in 1:2) { > + k <- i+1 > + for (j in k:3) { > + print(j) > + } > + } > [1] 2 > [1] 3 > [1] 3 > > > > This is under NT4, SP5 using a pre-compiled binary from CRAN. > > Thanks, > -jh- >-- __________________________________________________ [ ] [ Giovanni Petris GPetris at uark.edu ] [ Department of Mathematical Sciences ] [ University of Arkansas - Fayetteville, AR 72701 ] [ Ph: (501) 575-6324, 575-8630 (fax) ] [ http://definetti.uark.edu/~gpetris/ ] [__________________________________________________] -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._