title: How to extract a random sample with multiple conditions that vary by group?
body: <p>I have a cross-national data set where each respondent has at least one diary. The number of diaries per respondent and diary completion day varies by country. </p>
  
  <p>For example, in one country each respondent completed only 1 diary (half of the respondents completed only on a weekend, while the other half only on a weekday). In another country, each respondent completed 2 diaries (one weekend- one weekday), and in another one everyone completed 7 diaries (one for each day of the week). There are also surveys where some of the respondents returned 2 diaries, while others 3; and there are those where one everyone returned 4 diaries. The data look like this:</p>
  
  <pre><code>country_id&lt;-rep(1:4,c(8,8,14,10))
  diarist_id&lt;-c(11:18,rep(21:24,each=2),
                rep(31:32,each=7),
                rep(41:44,c(3,3,2,2)))
  diary_id&lt;-c(111:118,211,212,221,222,231,232,241,242,
              311:317,321:327,411,412,413,
              421,422,423,431,432,441,442)
  weekend&lt;-c(1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,
             0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,
             0,1,0,1,0,1,0,1,0)
  
  dat&lt;-data.frame(country_id,diarist_id,diary_id,weekend)
  </code></pre>
  
  <p>I am trying to draw a random sample of “one person-one diary” from each country. But at the country level I need -approximately- 29% of the diaries to be weekend diaries. How can I draw such a conditional random sample by group? </p>