Re: [R] questions about using loop, while and next

From: Berend Hasselman <bhh_at_xs4all.nl>
Date: Fri, 04 Mar 2011 01:00:23 -0800 (PST)

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.

______________________________________________
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 04 Mar 2011 - 09:02:38 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 04 Mar 2011 - 09:40:20 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