[R] Creating a loop with an indefinite end term

R. Michael Weylandt michael.weylandt at gmail.com
Tue Apr 10 17:34:13 CEST 2012


You might want to use a while loop instead, something like:

while(TRUE){
  # Do things
  # Test: if your condition has occured
  if(conditionHappened) break # break will end loop.
}

Michael

On Tue, Apr 10, 2012 at 10:48 AM, Steve Lavrenz
<stevelavrenz at hotmail.com> wrote:
> Everyone,
>
> I'm very new to R, especially when it comes to loops and functions, so
> please bear with me if this is an elementary question. I cannot seem to
> figure out how to construct a loop which runs a function until a certain
> value is computed. For example, say I have the following:
>
> num = numeric (10)
> num [1] = 0
> for (i in 2:10)   {
>      num [i] = num [i-1] + 5
> }
>
> This adds 5 to the preceding spot of a vector of length 10 to get the value
> in the current spot. However, say I don't just want to run this for 10
> spots; rather I want to run it until a certain value (say, 100) is computed.
> How I construct my loop to do this?
>
> Thanks!
>
>
>        [[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.



More information about the R-help mailing list