title: How to draw from sample until at least one of each sample value is obtained and then stop
body: <p>I am working on a homework problem where a cereal company is running a promotion where 1 of 4 free toys is included in each box. The goal is to collect all 4 toys through buying one box at a time. </p>
  
  <p>There are <strong>two scenarios</strong>:</p>
  
  <p>1.Each toy is <strong>equally likely</strong></p>
  
  <p>2.The toys have selection probabilities <strong>0.10, 0.25, 0.25, and 0.40</strong>.</p>
  
  <p>I want to design a function that takes each toy number and each toy's selection probability as inputs, simulates buying boxes of cereal by sampling from the toy numbers until at least one of each toy is obtained, then stops and reports how many boxes were purchased.</p>
  
  <p>The end goal is to use this function in a <code>Monte Carlo</code> simulation study to find out on average how many boxes consumers will have to buy to collect all the toys, and the proportion of consumers that will have to buy at least 14 boxes to collect all the toys.</p>
  
  <p>I've tried to create a <code>loop (while, repeat)</code> which samples until a vector contains all the toy values but the loops have run infinitely. I suspect there is a problem with the condition I am feeding the loop.</p>
  
  <pre><code>box_buyer &lt;- function (purchase_options, probabilities) {
  
    boxes &lt;- numeric()
  
    while (!purchase_options %in% boxes) {
  
      append(boxes, sample(purchase_options, 1, probabilities, replace = TRUE))
  
    }
  
    return(length(boxes))
  
  }
  
  box_buyer(c(1, 2, 3, 4), c(1/4, 1/4, 1/4, 1/4))
  </code></pre>
  
  <p>I am expecting a function that returns the number of boxes that were purchased. Currently what I get is an infinite loop that gives the error: <code>"the condition has length &gt; 1 and only the first element will be used"</code> repeated until I terminate <code>R</code>.</p>
  
  <p>How can I get the loop to sample until at least one of each toy is obtained and then stop and return the number of boxes purchased? Any help is appreciated.</p>