[R] really dumb question | loop counters in

Petr PIKAL petr.pikal at precheza.cz
Mon Sep 24 08:55:20 CEST 2007


Hi


> Paul Hiemstra wrote:
> > Hi,
> >
> > This works:
> >
> > for(i in seq(1,100,5)) {
> > print(i)
> > }
> >
> > Very similar to the way python does this kind of loop.
> >
> 
> Indeed it is - thanks for the tip. I'm still puzzled why I can't find a 
> single piece of the standard [R] language documentation that shows this. 

> In contrast, every single other language I use (more than I care to 
> admit), and documentation for same, feature this prominently when they 
> talk about looping.

Maybe that is because looping is not a core feature of R language. Many 
things for which you has to use loops in other languages can be solved in 
R by its functions operating instantly on whole objects (vectors, matices, 
data.frames, lists).

Besides from for help page

seq
An expression evaluating to a vector 
                                        ^^^^^^^^^^^^^^^^^ 
And you cen directly inspect to what your construction evaluates by using 
them

1:5:100
(0,1,0.1)

So you shall/can put any sequence/vector into a for cycle 

for(var in seq) expr

seq = 1:50
seq = seq(1,100,5)
seq = sample(whatever apropriate vector)
seq = vector of file names
seq = vector of object names

etc.

Regards
Petr

> 
> Ah well.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list