title: Repeat or while loop with map function in a simulation in r
body: <p>Below is my data where columns group, score and prob represent treatment groups, scores outcome and probability of occurrence of the score outcome</p>
  <pre><code>data1 &lt;- tibble(group = rep(c('A', 'B'), each = 5),
         score = c(0, 1, 2, 3, 4, 0, 1, 2, 3, 4), 
         prob = c(.08, .8, .1, .02, 0, 0, 0, .4, .4, .2)) 
  
  data1
  </code></pre>
  <p>I want to use this data and simulate the scores <code>R</code> times so I expand as follows</p>
  <pre><code>R &lt;- 1:10 # number of simulations
  
  data2 &lt;- data1 %&gt;%
    nest(data = c(score, prob)) %&gt;%
    unnest_wider(col = data) %&gt;%
    mutate(size = 15) 
  
  data3 &lt;- data2 %&gt;%
    group_by(group, score, prob, size) %&gt;%
    tidyr::expand(iter = R) %&gt;%
    ungroup 
  
  data3
  </code></pre>
  <p>My function to simulate the data is as follows  where I use multinomial distribution</p>
  <pre><code>sim_data &lt;- function(dt, n, size, prob){
    d1 &lt;- dt %&gt;%
      mutate(sim_score = pmap(list(n    = 1,
                                   size = size,
                                   prob = prob),
                              rmultinom)) %&gt;%
      select(-prob) %&gt;%
      unnest(cols = c(sim_score, score))
    
    # create real scores from counts above
    d2 &lt;- d1 %&gt;%
      rowwise %&gt;%
      mutate(outcome = map(score, rep, times = sim_score)) %&gt;%
      unnest(cols = outcome) %&gt;%
      select(size, contains('iter'), group, outcome) %&gt;%
      nest(data = c(group, outcome))
    
    return(d2)
  }
  
  data4 &lt;- sim_data(data3, n, size, prob)
  data4
  </code></pre>
  <p>This works fine however I want to omit those instances where each group has less than or equal to 2 unique scores. In this case, I modify my function <code>sim_data</code> to do the check as follows using <code>repeat</code> so as to ignore cases that do not meet the above mentioned requirement</p>
  <pre><code> sim_check &lt;- function(my_data, n, size, prob){
    
    repeat{
      # simulate data
      dt = sim_data(my_data, n, size, prob)
      
      # check requirement
      check &lt;- dt %&gt;%
        select(data) %&gt;%
        unnest(cols = c(data)) %&gt;%
        group_by(group) %&gt;%
        distinct(outcome) %&gt;%
        mutate(id = 1:n()) %&gt;%
        summarise(n = n()) %&gt;%
        distinct(n) %&gt;%
        pull(n)
      
      if(all(check &gt; 2)) break
    }
    return(dt)
  }
  
  data5 &lt;- sim_check(data3, n, size, prob)
  </code></pre>
  <p>This however does not do the checking as I see observations with 2 unique scores.</p>
  <p>I can achieve my objective with a for loop as follows but I want to avoid the for loop. Can someone point out where I'm doing wrong</p>
  <pre><code>out &lt;- list()
  
  for(i in 1:10){
    data6 &lt;- sim_check(data2, n, size, prob)
    out[[i]]  &lt;- data6 %&gt;%
      pull(data) 
  }
  </code></pre>
  <p>@Ronak thanks for the observation. I just corrected it</p>