[R] really dumb question | loop counters in

Duncan Murdoch murdoch at stats.uwo.ca
Sat Sep 22 01:15:05 CEST 2007


On 21/09/2007 6:54 PM, Evan Cooch wrote:
> Thanks, but  there is nothing in section 9.2.2 that mentions seq(x,y,z) 
> or anything close in a for loop.  All it says is (basically):
> 
> 
> There is also a for loop construction which has the form
>  > for (name in expr_1) expr_2
> where name is the loop variable. expr 1 is a vector expression, (often a 
> sequence like 1:20), and
> expr 2 is often a grouped expression with its sub-expressions written in 
> terms of the dummy
> name. expr 2 is repeatedly evaluated as name ranges through the values 
> in the vector result of
> expr 1.
> 
> Moreover, I would have assumed it would be in the language definition 
> file (not that I could find - I did check),

You seem to be assuming the language is different than it is.  To do the 
loop you want, you construct the vector of values you want to loop over, 
and loop over it.  There's no specific syntax for that, because there's 
no need for it.  There's just a for loop that loops over a general 
vector.  You can put anything you want in that vector.

  the reference manual (nada),
> and so forth. If someone can point to the precise page in one of the 
> standard - distributed - bits of R documentation the specifically says 
> 'here is how you use a non-unity incremental counter in an iterative 
> loop in R', with an example, I'll stand corrected.

I'd look for that sort of thing in a tutorial on "R for programmers who 
already know XYZ" (for your particular choice of XYZ), if I didn't find 
it in the language reference.  Or ask on R-help, which you did, and you 
got the answer you were looking for.

Duncan Murdoch

> 
> Duncan Murdoch wrote:
>> On 21/09/2007 4:20 PM, Evan Cooch wrote:
>>> 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.
>> It's in "9.2.2 Repetitive execution: for loops, repeat and while" of 
>> the Introduction to R manual. That's a good manual to read if you're 
>> looking for an introduction to R.
>>
>> Duncan Murdoch
>>
>>



More information about the R-help mailing list