Code to determine amount of interest on a loan in R?

Viewed 28

I am trying to write a code to determine the amount owed on a loan after a certain amount of time but I can’t figure out how to get loops in R to work correct. Here’s my code so far:

x=10000 #amount borrowed
i=6.5 #interest rate
counter <- 0
    repeat{
        counter=counter+1
        while(counter<=365){
            repeat{
                y=x+(x*(i/365))
                print(y)}
            if(counter>365){break}
        }
    }

It keeps only running the ‘y’ equation once instead of 365 times like I want.

1 Answers

There are a couple of issues here. First of all, the interest rate i is apparently being specified as a percentage. It needs to be divided by 100 in order to turn it into a proportion for the relevant calculations. Secondly, it is not clear how often the interest is being compounded. If it is being compounded once per day to result in a 6.5% APR,, then the equivalent daily interest rate would be (1 + i/100)^(1/365) - 1. Thirdly, there is no need for an explicit loop here. You could use Reduce instead. Lastly, if you need to run the calculation many times, then perhaps putting it inside a function would be your best bet:

calculate_interest <- function(loan, interest_rate, days) {
  
  round(Reduce(`*`, rep((1 + interest_rate/100)^(1/365), days), loan) - loan, 2)
}

For example, after 365 days, we get the expected answer of 650:

calculate_interest(loan = 10000, interest_rate = 6.5, days = 365)
#> [1] 650

To get the interest owed after 30 days we could do:

calculate_interest(loan = 10000, interest_rate = 6.5, days = 30)
#> [1] 51.89

And if you want to see all the values owed from days 1 to 365, it is best to get these in a vector instead of printing them to the console:

results <- sapply(1:365, function(x) {
  calculate_interest(loan = 10000, interest_rate = 6.5, days = x)
})

Now you can put the results in a data frame, perform further calculations, or simply view them at any time.

results
#>   [1]   1.73   3.45   5.18   6.90   8.63  10.36  12.08  13.81  15.54  17.27
#>  [11]  19.00  20.73  22.45  24.18  25.91  27.64  29.37  31.10  32.84  34.57
#>  [21]  36.30  38.03  39.76  41.49  43.23  44.96  46.69  48.43  50.16  51.89
#>  [31]  53.63  55.36  57.10  58.83  60.57  62.31  64.04  65.78  67.52  69.25
#>  [41]  70.99  72.73  74.47  76.20  77.94  79.68  81.42  83.16  84.90  86.64
#>  [51]  88.38  90.12  91.86  93.60  95.35  97.09  98.83 100.57 102.31 104.06
#>  [61] 105.80 107.55 109.29 111.03 112.78 114.52 116.27 118.01 119.76 121.51
#>  [71] 123.25 125.00 126.75 128.49 130.24 131.99 133.74 135.49 137.23 138.98
#>  [81] 140.73 142.48 144.23 145.98 147.73 149.49 151.24 152.99 154.74 156.49
#>  [91] 158.24 160.00 161.75 163.50 165.26 167.01 168.77 170.52 172.28 174.03
#> [101] 175.79 177.54 179.30 181.05 182.81 184.57 186.33 188.08 189.84 191.60
#> [111] 193.36 195.12 196.88 198.64 200.40 202.16 203.92 205.68 207.44 209.20
#> [121] 210.96 212.72 214.48 216.25 218.01 219.77 221.54 223.30 225.06 226.83
#> [131] 228.59 230.36 232.12 233.89 235.65 237.42 239.19 240.95 242.72 244.49
#> [141] 246.26 248.02 249.79 251.56 253.33 255.10 256.87 258.64 260.41 262.18
#> [151] 263.95 265.72 267.49 269.26 271.04 272.81 274.58 276.35 278.13 279.90
#> [161] 281.67 283.45 285.22 287.00 288.77 290.55 292.32 294.10 295.87 297.65
#> [171] 299.43 301.21 302.98 304.76 306.54 308.32 310.10 311.87 313.65 315.43
#> [181] 317.21 318.99 320.77 322.55 324.34 326.12 327.90 329.68 331.46 333.25
#> [191] 335.03 336.81 338.60 340.38 342.16 343.95 345.73 347.52 349.30 351.09
#> [201] 352.88 354.66 356.45 358.24 360.02 361.81 363.60 365.39 367.18 368.96
#> [211] 370.75 372.54 374.33 376.12 377.91 379.70 381.50 383.29 385.08 386.87
#> [221] 388.66 390.45 392.25 394.04 395.83 397.63 399.42 401.22 403.01 404.81
#> [231] 406.60 408.40 410.19 411.99 413.79 415.58 417.38 419.18 420.98 422.77
#> [241] 424.57 426.37 428.17 429.97 431.77 433.57 435.37 437.17 438.97 440.77
#> [251] 442.57 444.38 446.18 447.98 449.78 451.59 453.39 455.19 457.00 458.80
#> [261] 460.61 462.41 464.22 466.02 467.83 469.63 471.44 473.25 475.05 476.86
#> [271] 478.67 480.48 482.29 484.09 485.90 487.71 489.52 491.33 493.14 494.95
#> [281] 496.76 498.58 500.39 502.20 504.01 505.82 507.64 509.45 511.26 513.08
#> [291] 514.89 516.70 518.52 520.33 522.15 523.97 525.78 527.60 529.41 531.23
#> [301] 533.05 534.87 536.68 538.50 540.32 542.14 543.96 545.78 547.60 549.42
#> [311] 551.24 553.06 554.88 556.70 558.52 560.34 562.16 563.99 565.81 567.63
#> [321] 569.46 571.28 573.10 574.93 576.75 578.58 580.40 582.23 584.06 585.88
#> [331] 587.71 589.54 591.36 593.19 595.02 596.85 598.67 600.50 602.33 604.16
#> [341] 605.99 607.82 609.65 611.48 613.31 615.14 616.98 618.81 620.64 622.47
#> [351] 624.31 626.14 627.97 629.81 631.64 633.48 635.31 637.15 638.98 640.82
#> [361] 642.65 644.49 646.33 648.16 650.00

Created on 2022-09-24 with reprex v2.0.2

Related