title: R repeat function until condition met
body: <p>I am trying to generate a random sample that excludes certain "bad data."  I do not know whether the data is "bad" until after I sample it.  Thus, I need to make a random draw from the population and then test it.  If the data is "good" then keep it.  If the data is "bad" then randomly draw another and test it.  I would like to do this until my sample size reaches 25.  Below is a simplified example of my attempt to write a function that does this.  Can anyone please tell me what I am missing?</p>
  
  <pre><code>df &lt;- data.frame(NAME=c(rep('Frank',10),rep('Mary',10)), SCORE=rnorm(20))
  df
  
  random.sample &lt;- function(x) {
    x &lt;- df[sample(nrow(df), 1), ]
    if (x$SCORE &gt; 0) return(x)
   #if (x$SCORE &lt;= 0) run the function again
  }
  
  random.sample(df)
  </code></pre>