[R] questions about using loop, while and next

Berend Hasselman bhh at xs4all.nl
Fri Mar 4 10:00:23 CET 2011


Carrie Li wrote:
> 
> ...
> In my loop, I have some random generation of data, but if the data doesn't
> meet some condition, then I want it to go next, and generate data again
> for
> next round.
> 
> # just an example..
> # i want to generate the data again, if the sum is smaller than 25
> temp=rep(NA, 10)
> for(i in 1:10)
> {
> dt=sum(rbinom(10, 5, 0.5))
> while (dt<25) next
> temp[i]=dt
> }
> 
> I also tried while(dt<25) {i=i+1}
> But it doesn't seem right to me, since it running nonstop. Any solutions ?
> ...
> 

You don't need next.
I think you mean this

temp <- rep(NA, 10) 
for(i in 1:10) { 
    dt <- 0 
    while (dt<25) dt <- sum(rbinom(10, 5, 0.5))
    temp[i] <- dt 
} 


/Berend

--
View this message in context: http://r.789695.n4.nabble.com/questions-about-using-loop-while-and-next-tp3334692p3334880.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list