# really dumb question | loop counters in

12 messages
Open this post in threaded view
|

## really dumb question | loop counters in

 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; valuenext) { ... } 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: really dumb question | loop counters in

 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; valuenext) { ... } > > > 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. > > ______________________________________________ > [hidden email] 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______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: really dumb question | loop counters in

 In reply to this post by cooch17 Try: for(x in seq(0,1,by=0.01)) { print(x) } The for loop in S/R is what some languages call a foreach loop, you need to provide a vector of the values to loop over. If you really want a C style for loop, then just realize that the for loop is a shorthand while loop: x <- 0 while( x < 1 ) {   print(x)   x <- x + 0.01 } Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [hidden email] (801) 408-8111     > -----Original Message----- > From: [hidden email] > [mailto:[hidden email]] On Behalf Of Evan Cooch > Sent: Friday, September 21, 2007 10:00 AM > To: [hidden email] > Subject: [R] really dumb question | loop counters in > > 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 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. > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: really dumb question | loop counters in

 Thanks. And thanks for the C-style tip. Greg Snow wrote: > Try: > > for(x in seq(0,1,by=0.01)) { > print(x) > } > > The for loop in S/R is what some languages call a foreach loop, you need > to provide a vector of the values to loop over. > > If you really want a C style for loop, then just realize that the for > loop is a shorthand while loop: > > x <- 0 > while( x < 1 ) { >   print(x) >   x <- x + 0.01 > } > > Hope this helps, > > > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: really dumb question | loop counters in

 In reply to this post by Paul Hiemstra 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. Ah well. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: really dumb question | loop counters in

 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: really dumb question | loop counters in

 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), 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. 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 > > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: really dumb question | loop counters in

 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 >> >> ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: really dumb question | loop counters in

 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. 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). Regardless, thanks for your help. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: really dumb question | loop counters in

 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.