This is the final article of the series on how environments work in R. If you haven’t read the previous articles, I recommend you to do that first. Follow these links:

Now that we understand the R evaluation model and the <<- operator, we can code up the Mortgage Payment Calculator in a different fashion.

Say you want to buy a $350,000 house, and you plan to get a 30-year fixed mortage (interest rate 4.87%) with a 20% down payment. We can use function amortize to
calculate the monthly mortgage payment, broken down by the interest portion and the principal portion. For example, let’s calculate the payment schedule for the first 12 months.

month

interest

principal

balance

1

1136.33

344.60

279655.4

2

1134.93

346.00

279309.4

3

1133.53

347.40

278962.0

4

1132.12

348.81

278613.2

5

1130.71

350.23

278263.0

6

1129.28

351.65

277911.3

7

1127.86

353.08

277558.2

8

1126.42

354.51

277203.7

9

1124.99

355.95

276847.8

10

1123.54

357.39

276490.4

11

1122.09

358.84

276131.5

12

1120.63

360.30

275771.2

This concludes the series on how environment works in R. Drop your questions and comments below.