title: Using sample() in R in a conditional way
body: <p>I want to assign a random number of B for each number of A.
  The condition is that the numer assigned of B must be bigger than the corresponding number of A.
  Here is my code.
  Is there any way to do this faster?<br>
  I tried with the for loop and other ways with a worst result!
  Thanks a lot!  </p>
  
  <pre><code>set.seed(123)
  A &lt;- rlnorm(10000,5,1) 
  B &lt;- rlnorm(100000,10,2)
  df &lt;- data.frame(NumPol=1:length(B), Cap=B)
  
  pol_sel &lt;- sapply(1:length(A), FUN = function(i) { 
  sample(df$NumPol[which(df$Cap &gt; A[i])], size=1, replace=TRUE) } )
  </code></pre>