title: Using loop to add columns in R
body: <p>My data set currently looks like this:</p>
  
  <pre><code>Contract number    FA      NAAR        q    
  CM300             9746     47000    0.5010
  UL350            80000       0      0.01234
  RAD3421          50000     10000    0.9431
  </code></pre>
  
  <p>I would like to add a column with a randomly generated number (called trial) between 0-1 for each row, and compare this number to the value in column q with another column saying 'l' if q &lt; trial, and 'd' if q > trial.</p>
  
  <p>This is my code which accomplishes this task one time. </p>
  
  <pre><code>trial &lt;- runif(3, min = 0, max = 1)
  data2 &lt;- mutate(data, trial)
  data2 &lt;- mutate(data, qresult = ifelse(data2$q &lt;= data2$trial, 'l', 'd'))
  </code></pre>
  
  <p>My struggle is to get this to repeat for several trials , adding new columns onto the table with each repetition. I have tried several types of loops, and looked through several questions and cannot seem to figure it out. I am fairly new to R, so any help would be appreciated!</p>