title: sample() command is too slow in R
body: <p>I want to create a random subset of a <code>data.table</code> <code>df</code> that is very large (around 2 million lines). 
  The data table has a weight column, <code>wgt</code> that indicates how many observation each line represents. 
  To generate the vector of row numbers I want to extract, I proceed as follows:</p>
  
  <p>I get the exact number of observations :</p>
  
  <pre><code>ns&lt;- length(df$wgt)
  </code></pre>
  
  <p>I get the number of desired lines (30% of the sample):</p>
  
  <pre><code>lines&lt;-round(0.3*ns)
  </code></pre>
  
  <p>I compute the vector of probabilities:</p>
  
  <pre><code>pr&lt;-df$wgt/sum(df$wgt)
  </code></pre>
  
  <p>And then I compute the vector of line numbers to get the subsample: </p>
  
  <pre><code>ssout&lt;-sample(1:ns, size=lines, probs=pr)
  </code></pre>
  
  <p>The final aim is to subset the data using <code>df[ssout,]</code>. However, R gets stuck when computing <code>ssout</code>. </p>
  
  <p>Is there a faster/more efficient way to do this? </p>
  
  <p>Thank you! </p>