I do not fully understand what you mean by "stop". If you mean
terminate
the whole function, then something like
sim <- function(nn, mustExist=100){
for (ii in 1:nn){
ee <- rep(rbinom(6000, 200, .5), ii)
if( any(ee!=mustExist) )
stop( paste("Iteration", ii, "did not contain",
mustExist,
". Terminating function \n") )
}
## Do something further ##
}
But generally, I want to just resample again till I get the desired length.
sim <- function(nn, mustExist=100){
counter <- 0; ii <- 1
while( counter <= nn ){
ee <- rep(rbinom(6000, 200, .5), ii)
if( any(ee!=mustExist) ){
warning(paste( "Iteration", ii, "did not
contain", mustExist,
". Resampling again\n") )
} else {
counter <- counter + 1
ii <- ii + 1
## Do something further ##
}
}
}
You can turn off the branch that returns the warnings if it gets annoying.
BTW, why do you want to use rep(.., ii) ?
Regards, Adai
If you want to count how many times out
On Mon, 2006-02-06 at 09:03 -0800, Matthew MacManes
wrote:> Hi R-Help,
>
> I'm trying a develop a test simulation where i evaluate the probability
> of not getting a value of 100 from the function rbinom(6000, 200, .5)
> [indeed, a very small probability]. At the end of each rep, I would
> like to evaluate the output, continue with the loop if the output
> contains the value 100, stop if the output lacks a 100.
>
> How do I get R to evaluate the output after each rep?
>
>
> >sim <- function(nn){
> > for (ii in 1:nn){
> > ee=rep(rbinom(6000, 200, .5), ii)
> > if (any(ee==100))
> > }
>
> Thanks,
> Matt MacManes
> ********************************************************
> Matthew D. MacManes
> PhD Student
> UC- Berkeley
> Department of Integrative Biology
> Museum of Vertebrate Zoology
> 3101 VLSB #3140
> Berkeley, CA 94720
> (510)642-7782
> EMAIL: macmanes at berkeley.edu
> WEBSITE: http://ib.berkeley.edu/labs/lacey/
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>