title: Creating a random sample from a dataframe with a nested structure
body: <p>This question builds from the SO post <a href="https://stackoverflow.com/questions/19989355/extracting-a-random-sample-of-rows-in-a-data-frame-with-a-nested-conditional">found here</a></p>
  
  <p>I am trying to extract a random sample of rows in a data frame using a nesting condition. </p>
  
  <p>Using the following dummy dataset (modified from <code>iris</code>):</p>
  
  <pre><code>  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          5.3         2.9          1.5         0.2  setosa
  5          5.2         3.7          1.3         0.2  virginica
  6          4.7         3.2          1.5         0.2  virginica
  7          3.9         3.1          1.4         0.2  virginica
  8          4.7         3.2          1.3         0.2  virginica
  9          4.0         3.1          1.5         0.2  versicolor
  10         5.0         3.6          1.4         0.2  versicolor
  11         4.6         3.1          1.5         0.2  versicolor
  12         5.0         3.6          1.5         0.2  versicolor
  </code></pre>
  
  <p>The code below works fine to take a simple sample of 2 rows: </p>
  
  <pre><code>iris[sample(nrow(iris), 2), ]
  </code></pre>
  
  <p>However, what I would like to do is to take a sample of 2 rows for each level of a specific variable. For example create a random sample of 2 rows for each level of the variable 'Species', like that:</p>
  
  <pre><code>  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  1          5.1         3.5          1.4         0.2  setosa
  4          5.3         2.9          1.5         0.2  setosa
  6          4.7         3.2          1.5         0.2  virginica
  7          3.9         3.1          1.4         0.2  virginica
  11         4.6         3.1          1.5         0.2  versicolor
  12         5.0         3.6          1.5         0.2  versicolor
  </code></pre>
  
  <p>Thanks for your help!</p>