[R] really dumb question | loop counters in

Charles C. Berry cberry at tajo.ucsd.edu
Sat Sep 22 19:13:33 CEST 2007


On Fri, 21 Sep 2007, Duncan Murdoch wrote:

> 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)".
>

Moreover, there are help pages that usually have explicit examples that a 
user can run to get a better sense of semantics and helpful constructions:

 	example("for")

will run the two examples on the "Control Flow" help page which

 	help("for")

will access.

Those examples seem to me to address the new user's need to "recognize 
that the argument is a vector, and how to specify the sequence over the 
vector" succinctly.

---

Mindful of the dictum of that 'the source code is the ultimate reference' 
(to which dictum I have no reference), there are about 10 instances of 
'seq( from, to, by )' found among 37 found with

  find ./R-beta/src -type f -name "*.R" -print0 | xargs -0 -e grep -n -e  "for[ ]*[(][^(]*seq[(]"

of almost 1800 instances found with just "for[ ]*[(]", which you may 
consult for useful hints about constructing for loops.

:-)

[rest deleted]

Chuck

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list