I was working on a project yesterday where I needed to amortize out a bunch of loans to calculate the total interest a borrower would pay if he or she paid the minimum monthly payment for the full term of the loan. I couldn’t find any package in R that already contained the necessary math, so I looked around and found this post as well as this one. They both presented the R code to do the basic math involved in amortization, but each function was built to handle only one loan at a time. I had well over 100,000 loans I needed to go through, and loops aren’t all that efficiently implemented in R.
So I revised the code to perform that math on all of the loans at once by organizing everything into matrices that could then be added, subtracted, etc. It only took a little over three seconds to amortize 110,335 loans. I don’t know how long it would have taken to amortize each loan individually – I killed the process after I got tired of waiting for it to finish.
The function takes the following parameters:
 p_input: the initial principal owed on the loan
 i_input: the interest rate
 n_months: the length of the loan term, in months
 output: format of the output; “list” returns a list of full amortization tables (balance, payment, principal, interest, and installment for each month); “table” combines all the individual tables into one and differentiates loans by a separate index column; “balance”, “payment” “principal”, and “interest” return only those columns
 index: an id number or other unique identifier for each loan; if not supplied, the loans are just numbered

Sorry it took me so long to get to this comment. The last row of the table now displays correctly. Thanks for pointing out the bug!
October 24, 2013 — 12:34
[…] Schaun Wheeler I was working on a project yesterday where I needed to amortize out a bunch of loans to calculate […]
Your function is so well estructurated but I was comparing with others amortization tables and in the last month there is a error. For example for a credit of 150000, annual interest of 12% and 12 months your function gives this:
amortization payment principal interest installment index
1 150000.00 13327.32 11827.32 1500.0000 1 1
2 138172.68 13327.32 11945.59 1381.7268 2 1
3 126227.09 13327.32 12065.05 1262.2709 3 1
4 114162.04 13327.32 12185.70 1141.6204 4 1
5 101976.34 13327.32 12307.55 1019.7634 5 1
6 89668.79 13327.32 12430.63 896.6879 6 1
7 77238.16 13327.32 12554.94 772.3816 7 1
8 64683.22 13327.32 12680.49 646.8322 8 1
9 52002.74 13327.32 12807.29 520.0274 9 1
10 39195.45 13327.32 12935.36 391.9545 10 1
11 26260.08 13327.32 13064.72 262.6008 11 1
12 13195.36 13195.36 13195.36 0.0000 12 1
That was the result in R, but made applying all concepts I got this:
Amortization Payment Principal Interest
1 150,000.00 13,327.32 11,827.32 1,500.00
2 138,172.68 13,327.32 11,945.59 1,381.73
3 126,227.09 13,327.32 12,065.05 1,262.27
4 114,162.04 13,327.32 12,185.70 1,141.62
5 101,976.34 13,327.32 12,307.55 1,019.76
6 89,668.79 13,327.32 12,430.63 896.69
7 77,238.16 13,327.32 12,554.94 772.38
8 64,683.22 13,327.32 12,680.49 646.83
9 52,002.74 13,327.32 12,807.29 520.03
10 39,195.45 13,327.32 12,935.36 391.95
11 26,260.08 13,327.32 13,064.72 262.60
12 13,195.36 13,327.32 13,195.36 131.95
You can see interest in last month is different of zero and payments are equal for all months. It is possible to make this in your funtion. Great job man! Awesome