title: Infinite recursion error - how to solve?
body: <p>Still new to R so trying to practice working with functions, I have created some (probably quite inefficient) code which I will look at modifying later, currently it returns the result I need but I want to repeat the simulation 100 times within the function itself.  This would effectively be turn[i] which returns outcomes -1 or 1 with a specified function probability:</p>
  
  <pre><code>game = function(n,pr) {
  turn = cumsum(2*rbinom(n,1,prob=pr)-1)
  bankrupcy.test = which(turn == -25)
  winner.test = which(turn == 50)
  if(length(bankrupcy.test)==0){bankrupcy.test=c(0)}
  if(length(winner.test)==0){winner.test=c(0)}
  if(bankrupcy.test==0 &amp;&amp; winner.test==0){turn[n]}else
  if(bankrupcy.test[1]&gt;winner.test[1]){-25}else{
  50}
  return(replicate(100,game(n)))
  }
  </code></pre>
  
  <p>I have tried creating a for loop but I can't seem to structure that correctly, hence I am looking to use the replicate command within the function I created, however I recieve the following error:</p>
  
  <p>"evaluation nested too deeply: infinite recursion / options(expressions=)?
  Error during wrapup: evaluation nested too deeply: infinite recursion /options(expressions=)"</p>
  
  <p>Where am I going wrong?  I want to return a vector with 100 outcomes of the above simulation, in which the game is played until 50 profit is made or a 25 loss is made, whichever occurs first.  If neither of the previous outcomes occur then the final value of the vector is taken.</p>