title: Random sample of rows from subset of an R dataframe
body: <p>Is there a good way of getting a sample of rows from part of a dataframe?</p>
  
  <p>If I just have data such as </p>
  
  <pre><code>gender &lt;- c("F", "M", "M", "F", "F", "M", "F", "F")
  age    &lt;- c(23, 25, 27, 29, 31, 33, 35, 37)
  </code></pre>
  
  <p>then I can easily sample the ages of three of the Fs with </p>
  
  <pre><code>sample(age[gender == "F"], 3)
  </code></pre>
  
  <p>and get something like </p>
  
  <pre><code>[1] 31 35 29
  </code></pre>
  
  <p>but if I turn this data into a dataframe</p>
  
  <pre><code>mydf &lt;- data.frame(gender, age) 
  </code></pre>
  
  <p>I cannot use the obvious</p>
  
  <pre><code>sample(mydf[mydf$gender == "F", ], 3)
  </code></pre>
  
  <p>though I can concoct something convoluted with an absurd number of brackets like </p>
  
  <pre><code>mydf[sample((1:nrow(mydf))[mydf$gender == "F"], 3), ]
  </code></pre>
  
  <p>and get what I want which is something like </p>
  
  <pre><code>  gender age
  7      F  35
  4      F  29
  1      F  23
  </code></pre>
  
  <p>Is there a better way that takes me less time to work out how to write?</p>