[R] error in Recursive

Berend Hasselman bhh at xs4all.nl
Tue Jan 10 09:29:03 CET 2012


arunkumar1111 wrote
> 
> Hi
>  
> I need help in the recursive problem.  this is my code
> 
> #Generate two random Numbers
> minval=20
> maxval=100
> cutoffValue=50
> 
> optVal<- function(cutoffValue,minval,maxval)
> {
>    x=runif(2)
>    x=x*cutoffValue
>    for( i in 1:2)
>     {
>       if(x[i] < 30 || x[i] >60)   # checking it falls between the range
>       {
>         optVal(cutoffValue,minval,maxval)
>       }
>    }
>    return(x)
> }
> 
> optVal(40,20,60)
> 
> I'm getting Error like this
> *Error: evaluation nested too deeply: infinite recursion /
> options(expressions=)?*
> 

You can easily find out what is wrong by inserting a print(x) just before
the if(...).
Why are you not using the minval and maxval arguments in your function
optVal?

Change the line with the if to

      if(x[i] < minval || x[i] > maxval)   # checking it falls between the
range 

and call optVal with a smaller value for minval 

optVal(40,20,60) 

and you will avoid the error message. Your minval of 30 is simply too high
i.e. you are rejecting an x too quickly.

Berend





--
View this message in context: http://r.789695.n4.nabble.com/error-in-Recursive-tp4281052p4281449.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list