title: Sample from probabilities based on column values for a large data.frame
body: <p>I can't find anything specific to my question so I hope someone can help me.
  I have a data frame (3 million + records ) that contains three columns that contain the probabilities of each "event" occurring. So a basic example for one record is the chance that A happens is 30%, B 20% and C 50%.</p>
  
  <p>So basically each observation has a probability of being A, B or C. This probability depends on the values in column A, B and C. </p>
  
  <p>Below is one way of ding it, where it samples from the columns A,B and C and then assign the sampled value to column "Answer". BUT for 3 million records it just wont work at all.</p>
  
  <pre><code>nRowsDf &lt;- nrow(df)
  for(i in 1:nRowsDf){
     df[i,c("Answer")] &lt;- sample(sample(c('A','B','C'), size = 1000,
       replace = TRUE, prob = c(df[i,"A_prop"],df[i,"B_prop"], df[i,"C_prop"]))
     ,1)
  }
  </code></pre>
  
  <p>I'm sure there's a better way than putting it in a slow loop that will run for +-30 hours.</p>
  
  <p>I have in the mean time switched to excel, not want to go there. But this is what I'm kind of doing in excel, with some modifications : <a href="https://www.extendoffice.com/documents/excel/4486-excel-random-with-probability.html" rel="nofollow noreferrer">How To Generate Random Value Based On Assigned Probability In Excel</a></p>