title: Getting random values between two column values in R
body: <p>I have a data frame which has two columns:</p>
  
  <pre><code>structure(list(lowage = c(45, 15, 9, 51, 22, 45, 4, 4, 9, 25), 
      highage = c(50, 21, 14, 60, 24, 50, 8, 8, 14, 30)), .Names = c("lowage", 
  "highage"), row.names = c(NA, 10L), class = "data.frame")
  </code></pre>
  
  <p>The data frame looks like:</p>
  
  <pre><code>   lowage highage
  1      45      50
  2      15      21
  3       9      14
  4      51      60
  5      22      24
  6      45      50
  7       4       8
  8       4       8
  9       9      14
  10     25      30
  </code></pre>
  
  <p>I am trying to get a random number for each row between the two columns and save it as a third column.</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>df$age &lt;- sample(df$lowage:df$highage,1)
  </code></pre>
  
  <p>This gives me the following error:</p>
  
  <pre><code>Error in `$&lt;-.data.frame`(`*tmp*`, age, value = c(47L, 50L, 49L, 48L,  : 
    replacement has 6 rows, data has 795
  In addition: Warning messages:
  1: In df$lowage:dfhighage :
    numerical expression has 795 elements: only the first used
  2: In dflowage:df$highage :
    numerical expression has 795 elements: only the first used
  </code></pre>
  
  <p>I tried with a for loop:</p>
  
  <pre><code>for (i in 1:length(df$lowage)) {
   df$age[i] &lt;- round(sample(df$lowage[i]:df$highage[i]),1)
  }
  </code></pre>
  
  <p>While this creates a column age with randomised age values, still it gives me the following warning:</p>
  
  <pre><code>Warning messages:
  1: In df$age[i] &lt;- round(sample(df$lowage[i]:df$highage[i]),  ... :
    number of items to replace is not a multiple of replacement length
  </code></pre>
  
  <p>Though I could see values for each row in my df, I am not sure if this warning have an impact on the column.</p>