title: Sample a subset of dataframe by group, with sample size equal to another subset of the dataframe
body: <p>Here's my hypothetical data frame;</p>
  
  <pre><code>location&lt;- as.factor(rep(c("town1","town2","town3","town4","town5"),100))
  visited&lt;- as.factor(rbinom(500,1,.4)) #'Yes or No' variable
  variable&lt;- rnorm(500,10,2)
  id&lt;- 1:500
  DF&lt;- data.frame(id,location,visited,variable)
  </code></pre>
  
  <p>I want to create a new data frame where the number of 0's and 1's are equal for each location.  I want to accomplish this by taking a random sample of the 0's for each location (since there are more 0's than 1's).</p>
  
  <p>I found this solution to sample by group;</p>
  
  <pre><code>library(plyr)
  ddply(DF[DF$visited=="0",],.(location),function(x) x[sample(nrow(x),size=5),])
  </code></pre>
  
  <p>I entered '5' for the size argument so the code would run, But I can't figure out how to set the 'size' argument equal to the number of observations where DF$visited==1.</p>
  
  <p>I suspect the answer could be in other questions I've reviewed, but they've been a bit too advanced for me to implement.</p>
  
  <p>Thanks for any help.</p>