title: Use Final Value In Iterative Process
body: <p>Here is the code that I am working with: </p>
  
  <pre><code>## Round 1
  b &lt;- c(5000,2500)
  r &lt;- c(2,2.1) 
  pm &lt;- c(200,240)
  
  I &lt;- b*(r/1200) 
  pi &lt;- pm-I
  end &lt;- b-pi  
  end
  
  ## Round 2 
  b1 &lt;- end 
  r1 &lt;- (2.3,2.3) 
  I1 &lt;- b1*(r/1200)
  pi1 &lt;- pm-I1 
  end1 &lt;- b1-pi1 
  end1
  </code></pre>
  
  <p>This produces the proper output, which is shown below in the post. But, you can see how this might get annoying for about 30 rounds so I thought that I would be able to use a *sapply function. So I attempted to do: </p>
  
  <pre><code>r2&lt;- matrix(c(2,2.1,2.3,2.3),nrow=2)
  I2 &lt;- sapply(b, function (x)
    x*(r2/1200)
  )
  
  pi2 &lt;- sapply(I, function (l)
    pm-l )
  
  
  end2 &lt;- b-pi2
  end2
  </code></pre>
  
  <p>Which gives: </p>
  
  <pre><code>[1,] 4808.333 4804.375
  [2,] 2268.333 2264.375
  </code></pre>
  
  <p>This did not produce the desired output which is: </p>
  
  <pre><code>[1,] 4808.333 4616.347
  [2,] 2264.375 2028.338
  </code></pre>
  
  <p>How exactly would I go about doing this because the "end" variables need the updated "b" value as shown in the starred part of the code below.  </p>
  
  <pre><code>    **b1 &lt;- end** 
      r1 &lt;- 2.3 
      I1 &lt;- b1*(r/1200)
      pi1 &lt;- pm-I1 
      end1 &lt;- b1-pi1 
      end1
  </code></pre>
  
  <p>Thank you for your time.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Pierre gives an answer below, but I have another question which is how would I do this process "n" number of times. Pierre's answer works for this specific question. But, I am trying to make this work when "r" and "r1" are joint in a matrix like</p>
  
  <pre><code>rate &lt;- rbind(r,r1) 
  </code></pre>
  
  <p>And then use an sapply function kind of like: </p>
  
  <pre><code>t(sapply(b, function(x) {b &lt;- b - (pm - b*(rate/1200));b}))
  </code></pre>
  
  <p>Any suggestions? </p>