Skip to content

for loop question

6 messages · john_heumann@agilent.com, Thomas Lumley, Ben Bolker +3 more

#
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:

            
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:

            

  
    
#
"HEUMANN,JOHN (A-Loveland,ex1)" wrote:
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
What 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>:
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 +.
[1] 2 3 4