[R] Creating a loop with an indefinite end term

Albyn Jones jones at reed.edu
Tue Apr 10 18:36:29 CEST 2012


What's wrong with

       num <- rep(0,10)
       done <- FALSE
       i <- 2
       while(!done){
		num[i] <- num[i-1] + 5
     		if(num[i] > 20) done <- TRUE
     		i <- i + 1
	}
       num <- num[1:(i-1)]

You can delete the unused tail when you finish.  Iteratively
reassigning to a vector of undefined length is possible too, but that
is much slower:

> x <- numeric()
> system.time(for(i in 1:100000){ x[i] <- i})
   user  system elapsed 
 29.641  27.509  57.163 

> y <- numeric(100000)
> system.time(for(i in 1:100000){ y[i] <- i})
   user  system elapsed 
  0.537   0.000   0.532 
> 

albyn


On Tue, Apr 10, 2012 at 11:53:08AM -0400, Steve Lavrenz wrote:
> Albyn,
> 
> Thanks for your help. This however, still assumes that I have to define an
> array of length 10. Is there a way that I can construct this so that my
> array is exactly as long as the number of spots I need to reach my threshold
> value?
> 
> Thanks,
> 
> -Steve
> 
> -----Original Message-----
> From: Albyn Jones [mailto:jones at reed.edu] 
> Sent: Tuesday, April 10, 2012 11:46 AM
> To: Steve Lavrenz
> Cc: r-help at r-project.org
> Subject: Re: [R] Creating a loop with an indefinite end term
> 
> Here are a couple of constructions that work.  
> 
> albyn
> ===========================================
> 
> num <- rep(0,10)
> for (i in 2:10)   { 
>       num[i] <- num[i-1] + 5 
>       if(num[i] > 20) break
> } 
> > num
>  [1]  0  5 10 15 20 25  0  0  0  0
> 
> or
> 
> num <- rep(0,10)
> done <- FALSE
> i <- 2
> while(!done){
>      num[i] <- num[i-1] + 5
>      if(num[i] > 20) done <- TRUE
>      i <- i + 1
> }
> > num
>  [1]  0  5 10 15 20 25  0  0  0  0
> 
> 
> On Tue, Apr 10, 2012 at 10:48:34AM -0400, Steve Lavrenz 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.
> > 
> 
> --
> Albyn Jones
> Reed College
> jones at reed.edu
> 
> 
> 

-- 
Albyn Jones
Reed College
jones at reed.edu



More information about the R-help mailing list