Re: [R] error in Recursive

2012-01-10 Thread Berend Hasselman

arunkumar 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.

__
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.


[R] error in Recursive

2012-01-09 Thread arunkumar1111
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=)?*

Please help me to solve this error.  This takes infinite loop

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

__
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.