title: Possible to avoid a FOR loop in this very simple R code?
body: <p>The answers below are very helpful. But I oversimplified my original question. I figured I learn more if I oversimplify and then adapt to my actual need, but now I am stuck. There are other factors that drive the amortization. See more complete code here. I like the response using &quot;amort$end_bal &lt;- begin_bal * (1 - mpr)^amort$period&quot; and &quot;amort$pmt &lt;- c(0, diff(amort$end_bal))* -1&quot;, but in addition <strong>npr</strong> increases the ending balances and <strong>ch_off</strong> decreases ending balances. Here´s the more complete code:</p>
  <pre><code>n_periods &lt;- 8
  begin_bal &lt;- 10000
  yld &lt;- .20
  npr &lt;- .09
  mpr &lt;- .10           
  co &lt;- .10            
  
  period = seq(0,n_periods,1)
  fin = 0
  pur = 0
  pmt = 0
  ch_off = 0
  end_bal = begin_bal
    
  for(i in 1:n_periods){
    {fin[i+1] = end_bal[i]*yld/12}
    {pur[i+1] = end_bal[i]*npr}
    {pmt[i+1] = end_bal[i]*mpr}
    {ch_off[i+1] = end_bal[i]*co/12}
    end_bal[i+1] = end_bal[i]+pur[i+1]-pmt[i+1]-ch_off[i+1]}
    
  amort &lt;- data.frame(period,fin,pur,pmt,ch_off,end_bal)
  </code></pre>
  <p>Which gives the below correct output:</p>
  <pre><code> print(amort,row.names=FALSE)
   period      fin      pur       pmt   ch_off   end_bal
        0   0.0000   0.0000    0.0000  0.00000 10000.000
        1 166.6667 900.0000 1000.0000 83.33333  9816.667
        2 163.6111 883.5000  981.6667 81.80556  9636.694
        3 160.6116 867.3025  963.6694 80.30579  9460.022
        4 157.6670 851.4020  946.0022 78.83351  9286.588
        5 154.7765 835.7929  928.6588 77.38823  9116.334
        6 151.9389 820.4700  911.6334 75.96945  8949.201
        7 149.1534 805.4281  894.9201 74.57668  8785.132
        8 146.4189 790.6619  878.5132 73.20944  8624.072
  </code></pre>
  <p>I´m new to R, and I understand one of its features is matrix/vector manipulation. In the below example I amortize an asset over 8 months, where each payment (&quot;pmt&quot;) is 10% (&quot;mpr&quot;) of the prior period balance (&quot;end_bal&quot;). The below works fine. I used a FOR loop. I understand FOR loops can be slow in large models and a better solution is use of R´s abundant vector/matrix functions. But I didn´t know how to do this in my example since each monthly payment is calculated by referencing the prior period ending balance.</p>
  <p>So my questions are:</p>
  <ol>
  <li>Is there a more efficient way to do the below?</li>
  <li>How do I replace the 0 for pmt in period 0, with an empty space?</li>
  </ol>
  <p>R code:</p>
  <pre><code>n_periods &lt;- 8                
  begin_bal &lt;- 100              
  mpr &lt;- .10                  
  
  # Example loan amortization
  pmt =     0
  end_bal = begin_bal
  for(i in 1:n_periods){
    {pmt[i+1] = end_bal[i]*mpr}
    end_bal[i+1] = end_bal[i]-pmt[i+1]}
  amort &lt;- data.frame(period = 0:n_periods,pmt,end_bal)
  amort
  </code></pre>
  <p>Results, which are correct:</p>
  <pre><code>&gt; amort
    period       pmt   end_bal
  1      0  0.000000 100.00000
  2      1 10.000000  90.00000
  3      2  9.000000  81.00000
  4      3  8.100000  72.90000
  5      4  7.290000  65.61000
  6      5  6.561000  59.04900
  7      6  5.904900  53.14410
  8      7  5.314410  47.82969
  9      8  4.782969  43.04672
  </code></pre>