[R] really dumb question | loop counters in

Evan Cooch cooch17 at verizon.net
Fri Sep 21 18:00:23 CEST 2007


Basically new to [R] - as a programming environment at least (had lots 
of recent experience compiling it on our Opteron-based servers). Was 
trying to write some simple little scripts (in advance of porting over 
some bigger things from other environments - like MATLAB), when I 
realized that handling counters in loop constructs in [R] is not 
patently obvious (at least, IMO, compared to other languages).

Suppose I want to iterate something from 1 to 100, using a step size of 
(say) 5. Trying the obvious

for(x in 1:5:100) {
print(x)
}

(Perhaps obviously, I've borrowed the MATLAB convention to some degree).

Or, looping from 0 -> 1 by 0.01?

I've dug through what [R] documentation I have, and all I can find is 
the somewhat obtuse.

For example, I can use

x <- seq(0,1, by=.01)

But not

for(x in (0,1,by=0.01)) {
print(x)
}

What about things that are slickly handled in C++, like

for (node=start; value<threshold && node!=end; node=node->next) { ... }


OK - I'm stumped (and happy to humiliate myself with what has surely got 
to be trivial).  I'm happy with a simple basic counter at this point.



More information about the R-help mailing list