[R] really dumb question | loop counters in

Paul Hiemstra p.hiemstra at geo.uu.nl
Fri Sep 21 18:22:48 CEST 2007


Hi,

This works:

for(i in seq(1,100,5)) {
print(i)
}

Very similar to the way python does this kind of loop.

Paul

Evan Cooch schreef:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>   


-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:     +31302535773
Fax:    +31302531145
http://intamap.geo.uu.nl/~paul



More information about the R-help mailing list