title: apply function not changing original value
body: <p>Related to  question on this page: <a href="https://stackoverflow.com/questions/25221199/randomly-associate-elements-of-two-vectors-given-conditions">Randomly associate elements of two vectors given conditions</a>
  If I have following data:</p>
  
  <pre><code>loss=c(45,10,5,1)
  
  capitals = structure(list(capital = c(100L, 50L, 4L, 25L, 5L), loss = c(5L, 
  10L, 10L, 1L, 45L)), .Names = c("capital", "loss"), class = "data.frame", row.names = c(NA, 
  -5L))
  
  capitals
    capital loss
  1     100    5
  2      50   10
  3       4   10
  4      25    1
  5       5   45
  &gt; 
  </code></pre>
  
  <p>I am trying to correct any row with loss>capital (assign another random value from vector loss so that loss&lt;=capital) by following command:</p>
  
  <pre><code>apply(capitals, 1, function(x){while(x[2]&gt;x[1]) {x[2] = sample(loss,1); print(x[2])} })
  </code></pre>
  
  <p>print function shows that the value is changing in the function but value is not changing in dataframe capitals:</p>
  
  <pre><code>apply(capitals, 1, function(x){while(x[2]&gt;x[1]) {x[2] = sample(loss,1); print(x[2])} })
  loss 
     5 
  loss 
    10 
  loss 
    10 
  loss 
     1 
  loss 
     5 
  NULL
  &gt; capitals
    capital loss
  1     100    5
  2      50   10
  3       4   10
  4      25    1
  5       5   45
  &gt; 
  </code></pre>
  
  <p>Why is value in capitals dataframe not changing and how can this be corrected? Thanks for your help.</p>