title: Sampling small data frame from a big dataframe
body: <p>I am trying to sample a data frame from a given data frame such that there are enough samples from each of the levels of a variable.
  This can be achieved by separating the data frame by the levels and sample from each of those .
  I thought <code>ddply</code> (data-frame to data-frame) would do it for me.
  Taking a minimal example:</p>
  <pre><code>set.seed(1)
  data1 &lt;-data.frame(a=sample(c('B0','B1','B2'),100,replace=TRUE),b=rnorm(100),c=runif(100))
  &gt; summary(data1$a)
  B0 B1 B2 
  30 32 38
  </code></pre>
  <p>The following commands perform the sampling...</p>
  <p>When I enter...</p>
  <pre><code>data2 &lt;- ddply(data1,c('a'),function(x) sample(x,20,replace=FALSE))
  </code></pre>
  <p>I get the following error</p>
  <blockquote>
  <p>Error in <code>[.data.frame</code>(x, .Internal(sample(length(x), size, replace,  :
  cannot take a sample larger than the population when 'replace = FALSE'</p>
  </blockquote>
  <p>This error is because <code>x</code> inside the <code>ddply</code> function is not a vector but a dataframe.</p>
  <p>Does anyone have any idea on how to achieve this sampling?
  I know one way is to not use ddply and just do (1) segregation, (2) sampling, and (3) collation in three steps. But I was wondering there must by some way ...with base or <code>plyr</code> functions...</p>
  <p>Thank you for your help...</p>