On Thu, 22 May 2014 09:11:43 PM Ricardo Rocha wrote:
> Hi everybody.
> 
> Consider the following exampling code:
> 
> x=numeric()
>   for(i in 1:10){
>     u=runif(1,-1,2)
>     x[i]=log(u)
>   }
> This code, in each interation, generates a random value in the (-1,2)
> interval and then calculates the log of the value. When the generated 
value
> is less than 0 the log produces a NaN, which gives a warning.
> 
> What I want is to make it start over when a warning is produced, in 
order to
> repeat it until a positive value is generated and therefore the log is
> calculated. Logically, would be like: "if there's a warning here, go back
> at the beginning and start over", without changing the iteration.
> 
> Could someone help me with some directions?
> 
Hi Ricardo,
Perhaps what you want is something like this:

i<-1
while(I < 11) {
 u<-runif(1,-1,2)
 if(u >= 0) {
  x[i]<-log(u)
  i<-i+1
 }
}

Jim

______________________________________________
R-help@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.

Reply via email to