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<threshold && node!=end; 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. |
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; value<threshold && node!=end; 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. > -- 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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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<threshold && node!=end; > 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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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. > > 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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
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:[hidden email] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 ______________________________________________ [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. |
In reply to this post by cooch17
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. > > ______________________________________________ > [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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Powered by Nabble | Edit this page |