Skip to content
Prev 13503 / 398502 Next

for loop question

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: