[R] where/what is i? for loop (black?) magic

Liaw, Andy andy_liaw at merck.com
Wed Jun 17 20:23:19 CEST 2009


A colleague and I were trying to understand all the possible things one
can do with for loops in R, and found some surprises.  I think we've
done sufficient detective work to have a good guess as to what's going
on "underneath", but it would be nice to get some confirmation, and
better yet, perhaps documentation in the R-lang manual.  Basically, the
question is, how/what does R do with the loop index variable?  Below are
some examples:

R> for (i in 1:2) { i <- 17; print(i) }
[1] 17
[1] 17
R> print(i)
[1] 17
R> x <- 1:2
R> for (i in x) { print(i); rm(i) }
[1] 1
[1] 2
R> i
Error: object 'i' not found
R> for (i in x) { print(i); rm(x) }
[1] 1
[1] 2
Warning message:
In rm(x) : object 'x' not found
R> i
[1] 2
R> x <- 1:2
R> for (i in x) { print(i); i <- 17; print(i) }
[1] 1
[1] 17
[1] 2
[1] 17

The guess is that at the beginning for the loop, R makes a copy of the
object that's being looped over ("x" in examples above) somewhere "under
cover", and at the beginning of each iteration, assign the "current"
element to the index variable ("I" in the examples above).  This is the
only logical explanation I can come up with given the behavior observed
above.  Can anyone confirm/deny this?  If this is true, one thing to
consider is not to use a large object to loop over (e.g., columns of a
very large data frame).

Andy
Notice:  This e-mail message, together with any attachme...{{dropped:12}}




More information about the R-help mailing list