title: Extracting a random sample of rows in a data.frame with a nested conditional
body: <p>This question builds from the SO post  <a href="https://stackoverflow.com/questions/8273313/random-rows-in-dataframe-in-r">found here</a> and uses code that was modified from a post on the R-help mailing list which can be <a href="https://stat.ethz.ch/pipermail/r-help/2007-February/125860.html" rel="nofollow noreferrer">seen here</a> </p>
  
  <p>I am trying to extract a random sample of rows in a data frame but with a conditional.  Using the R <code>iris</code> data which looks like:</p>
  
  <pre><code>&gt; head(iris)
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  1          5.1         3.5          1.4         0.2  setosa
  2          4.9         3.0          1.4         0.2  setosa
  3          4.7         3.2          1.3         0.2  setosa
  4          4.6         3.1          1.5         0.2  setosa
  5          5.0         3.6          1.4         0.2  setosa
  6          5.4         3.9          1.7         0.4  setosa 
  </code></pre>
  
  <p>To take a simple random sample, the code below works fine to take a sample of 2 rows.  </p>
  
  <pre><code>iris[sample(nrow(iris), 2), ]
  </code></pre>
  
  <p>However I am unsure how to condition the Species field.  For example how to take the random sample as indicated above but only when <code>Species != “setosa”</code></p>
  
  <p>There are three categories of <code>iris$Species</code></p>
  
  <pre><code>&gt; summary(iris$Species)
      setosa versicolor  virginica 
          50         50         50
  </code></pre>
  
  <p>I am unsure how to correctly nest conditionals.  One of my earlier attempts is below with the obviously incorrect results included….</p>
  
  <pre><code>&gt; iris[sample(nrow(iris)[iris$Species != "setosa"], 2), ]
       Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  NA             NA          NA           NA          NA    &lt;NA&gt;
  NA.1           NA          NA           NA          NA    &lt;NA&gt;
  </code></pre>
  
  <p>Thanks</p>