title: sample from data.table
body: <p>I have some data.table from which I want to select a random subset, but only for some operations. </p>
  
  <p>Suppose the data is</p>
  
  <pre><code>dat &lt;- data.table(id=1:100, group=sample(1:20,100, replace=TRUE), a=runif(100), b=rnorm(100))
  </code></pre>
  
  <p>and I want to do two things:</p>
  
  <ol>
  <li>count the number of ids per group</li>
  <li>select from each group one id at random and record its value on <code>a</code> and <code>b</code> </li>
  </ol>
  
  <p>I could follow <a href="https://stackoverflow.com/questions/24685421/how-do-you-extract-a-few-random-rows-from-a-data-table-on-the-fly">How do you extract a few random rows from a data.table on the fly</a> and choose </p>
  
  <pre><code>dat[n=.N, a=a[sample(.N,1)], b=b[sample(.N,1)], group]
  </code></pre>
  
  <p>but I am afraid, this will select <code>a</code> and <code>b</code> independently from one another. Is there a way of selecting the same?</p>