[R] really dumb question | loop counters in

Duncan Murdoch murdoch at stats.uwo.ca
Sat Sep 22 02:15:58 CEST 2007


On 21/09/2007 7:41 PM, Evan Cooch wrote:
> 
> Duncan Murdoch wrote:
>> 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.
>>
>>
> Point being, the documentation makes the implicit assumption that the 
> new user will immediately recognize that the argument is a vector, and 
> how to specify the sequence over the vector.

That's no assumption:  it's stated explicitly, even with an example 
"expr 1 is a vector expression, (often a sequence like 1:20)".

There is an assumption that this isn't the only part of the 
documentation you've read, that you're familiar with the basics of the 
language, but don't know how to do loops.  How to construct regular 
sequences was defined way back in section 2.2.

It's a difficult problem to write documentation for people who already 
know another language.  They already know how to do things, so they 
don't want to read from the beginning:  it's too boring.  But they also 
bring misconceptions with them, like the idea that a loop with a 
non-integer step size should be something supported by the syntax of the 
language.

I would not choose to add an example here using seq() (why that 
particular example?  why not loop over the letters of the alphabet, or 
the states in the US, or the components of a complex list?  We don't 
want people to think for loops are as limited as in some other 
languages).  Perhaps stating explicitly that you can construct the 
expr_1 vector any way you like would be good.

If you want to contribute a rewrite of that section of the manual, you 
can get the original from 
https://svn.r-project.org/R/trunk/doc/manual/R-intro.texi.  I'll take a 
look at whatever you write, and commit it if it looks better than what's 
there.  (If I don't commit, I'll tell you why not.)

  This is a good example of
> what I call obtuse documentation (having written ~1100 pages of 
> documentation for various opensource programs, I'm sort of sensitive to 
> this).

There's no question the documentation for R could be improved.

Duncan Murdoch



More information about the R-help mailing list