title: Sampling using an id variable
body: <p>I know how to do ordinary random sampling using R:</p>
  
  <pre><code>mysample &lt;- mydata[sample(1:nrow(mydata), 100),]
  </code></pre>
  
  <p>However, I want to sample by id variables. Let me explain - my dataset looks like this:</p>
  
  <pre><code>id var1 var2 ...
  1  5.1  1.2
  1  4.7  0.9
  2  3.3  1.6
  3  3.4  5.7
  4  7.9  1.3
  </code></pre>
  
  <p>Now, I want to take a random sample of, say, 2, by id numbers. Let's say the random sample yields id 1 and 4, then my sample would look like this:</p>
  
  <pre><code>id var1 var2 ...
  1  5.1  1.2
  1  4.7  0.9
  4  7.9  1.3
  </code></pre>
  
  <p>In other words, I'm sampling 2 id numbers, but I'm actually getting 3 cases.</p>
  
  <p>How can I accomplish this in R?</p>