[R] for loop query

Claudia Beleites cbeleites at units.it
Tue Dec 9 13:20:04 CET 2008


Hi,

> Why isn't my loop incrementing i - the outer loop to 2 and then resetting
> j=3?
It is. It runs out of bounds with j > 26

> Am I missing something obvious?
>          > for (i in 1:25)
>          + {
>          +   for (j in i+1:26)
You miss parentheses.

i + 1 : 26  is i + (1 : 26) as the vector 1 :26 is calculated first

what happens is that for i = 1 j goes over 2 : 27, with i = 2 over 3 : 28, ...

what you want is (i + 1) : 26:

for (i in 1 : 25)
   for (j in (i + 1) : 26)
      cat (i, j, "\n")

HTH Claudia

-- 
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I-34127 Trieste

phone: +39 (0 40) 5 58-34 47
email: cbeleites at units.it



More information about the R-help mailing list