Re: [R] for loop help

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Fri, 11 Apr 2008 09:57:21 -0600

Tom,

Bill Venables gave you references to important tools for dealing with for loops in R and they may be all the solution that you need. But here is a little more detail on what is going on in case you want/need more control in the future.

Note that the R for loop is what some programers call a foreach loop, meaning that rather than specifying an init, a test, and an increment (like C), you give the loop a vector of values and it loops through that vector using all the values regaurdless of what changes are made.

In C, the for loop:

for( init; test; inc ){ ... }

Is just shorthand for:

init;
while( test ){
 ...
 inc
}

R also has the while loop, so you can write a C style for loop in R by doing something like:

> i <- 1
> while( i <= 13 ){

+ print(i)
+ i <- i + 1
+ }

>

And if you insert i=12 into that, then it will behave how you describe.

'next' and 'break' also work with while loops to give even more control/options.

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow_at_imail.org
(801) 408-8111
 
 


> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of
> Bill.Venables_at_csiro.au
> Sent: Thursday, April 10, 2008 9:38 PM
> To: tom.soyer_at_gmail.com; r-help_at_r-project.org
> Subject: Re: [R] for loop help
>
> > ?`break`
> > ?`next`
>
> > for(i in 1:13) {
> if(i < 13) next
> print("Hello!\n")
> }
> [1] "Hello!\n"
> >
>
>
> Bill Venables
> CSIRO Laboratories
> PO Box 120, Cleveland, 4163
> AUSTRALIA
> Office Phone (email preferred): +61 7 3826 7251 Fax (if
> absolutely necessary): +61 7 3826 7304
> Mobile: +61 4 8819 4402
> Home Phone: +61 7 3286 7700
> mailto:Bill.Venables_at_csiro.au
> http://www.cmis.csiro.au/bill.venables/
>
> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org]
> On Behalf Of tom soyer
> Sent: Friday, 11 April 2008 12:26 PM
> To: r-help_at_r-project.org
> Subject: [R] for loop help
>
> Hi,
>
> I am trying to find a solution in R for the following C++
> code that allows one to skip ahead in the loop:
>
> for (x = 0; x <= 13; x++){
> x=12;
> cout << "Hello World";
> }
>
> Note that "Hello World" was printed only twice using this C++
> loop. I tried to do the same in R:
>
> for(i in 1:13){
> i=12
> print("Hello World")
> }
> It doesn't work as I expected, i.e., this R loop prints
> "Hello World" 13 times.
>
> Does anyone know how to do it in R?
>
> Thanks,
>
> --
> Tom
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org 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.
>
> ______________________________________________
> R-help_at_r-project.org 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.
>
______________________________________________ R-help_at_r-project.org 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.
Received on Fri 11 Apr 2008 - 16:07:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Apr 2008 - 16:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive