R语言小代码7(流程控制练习)

任务:计算等额本金还款和等额本息还款
小张贷款100万购买房子,贷款利率为4.9%(年利率),贷款期限20年。
1)用循环执行语句(for或者while),分别计算等额本金还款和等额本息还款的每月偿还本金、偿还利息、偿还本息、剩余本金
2)以数据框的形式给出。
3)计算两种还款方式的利息差额。

代码:

输入数据:

> #输入数据
> B <- 1000000
> R <- 0.049/12 #月利率
> t <- 20*12 #分t期还款
> N <- 1:t 

等额本金:

> #等额本金
> MBA <- B/t #每月偿还本金=贷款总额/贷款月数
> MYA <- B - MBA*(N-1) #每月剩余本金
> MIA <- MYA*R #每月偿还利息=剩余本金*月利率
> MPA <- MIA + MBA #每月偿还本息和
> one <- data.frame('等额本金每月偿还本金'=MBA,'等额本金每月偿还利息'=MIA,'等额本金每月偿还本息和'=MPA,'等额本金每月剩余本金'=MYA)
> one

上述代码没有for循环语句,添加后如下:

> one <- data.frame('等额本金每月偿还本金'=0,'等额本金每月偿还利息'=0,'等额本金每月偿还本息和'=0,'等额本金每月剩余本金'=0)
> one
  等额本金每月偿还本金 等额本金每月偿还利息 等额本金每月偿还本息和 等额本金每月剩余本金
1                    0                    0                      0                    0
> #等额本金
> MBA <- B/t #每月偿还本金=贷款总额/贷款月数
> for(i in N){
+   MYA <- B - MBA*(i-1) #每月剩余本金
+   MIA <- MYA*R #每月偿还利息=剩余本金*月利率
+   MPA <- MIA + MBA #每月偿还本息和
+   one[i,] <- data.frame('等额本金每月偿还本金'=MBA,'等额本金每月偿还利息'=MIA,'等额本金每月偿还本息和'=MPA,'等额本金每月剩余本金'=MYA)
+ }
> one
    等额本金每月偿还本金 等额本金每月偿还利息 等额本金每月偿还本息和 等额本金每月剩余本金
1               4166.667           4083.33333               8250.000          1000000.000
2               4166.667           4066.31944               8232.986           995833.333
3               4166.667           4049.30556               8215.972           991666.667
4               4166.667           4032.29167               8198.958           987500.000
5               4166.667           4015.27778               8181.944           983333.333
......

结果:

    等额本金每月偿还本金 等额本金每月偿还利息 等额本金每月偿还本息和 等额本金每月剩余本金
1               4166.667           4083.33333               8250.000          1000000.000
2               4166.667           4066.31944               8232.986           995833.333
3               4166.667           4049.30556               8215.972           991666.667
4               4166.667           4032.29167               8198.958           987500.000
5               4166.667           4015.27778               8181.944           983333.333
6               4166.667           3998.26389               8164.931           979166.667
7               4166.667           3981.25000               8147.917           975000.000
8               4166.667           3964.23611               8130.903           970833.333
9               4166.667           3947.22222               8113.889           966666.667
10              4166.667           3930.20833               8096.875           962500.000
11              4166.667           3913.19444               8079.861           958333.333
12              4166.667           3896.18056               8062.847           954166.667
13              4166.667           3879.16667               8045.833           950000.000
14              4166.667           3862.15278               8028.819           945833.333
15              4166.667           3845.13889               8011.806           941666.667
16              4166.667           3828.12500               7994.792           937500.000
17              4166.667           3811.11111               7977.778           933333.333
18              4166.667           3794.09722               7960.764           929166.667
19              4166.667           3777.08333               7943.750           925000.000
20              4166.667           3760.06944               7926.736           920833.333
21              4166.667           3743.05556               7909.722           916666.667
22              4166.667           3726.04167               7892.708           912500.000
23              4166.667           3709.02778               7875.694           908333.333
24              4166.667           3692.01389               7858.681           904166.667
25              4166.667           3675.00000               7841.667           900000.000
26              4166.667           3657.98611               7824.653           895833.333
27              4166.667           3640.97222               7807.639           891666.667
28              4166.667           3623.95833               7790.625           887500.000
29              4166.667           3606.94444               7773.611           883333.333
30              4166.667           3589.93056               7756.597           879166.667
31              4166.667           3572.91667               7739.583           875000.000
32              4166.667           3555.90278               7722.569           870833.333
33              4166.667           3538.88889               7705.556           866666.667
34              4166.667           3521.87500               7688.542           862500.000
35              4166.667           3504.86111               7671.528           858333.333
36              4166.667           3487.84722               7654.514           854166.667
37              4166.667           3470.83333               7637.500           850000.000
38              4166.667           3453.81944               7620.486           845833.333
39              4166.667           3436.80556               7603.472           841666.667
40              4166.667           3419.79167               7586.458           837500.000
41              4166.667           3402.77778               7569.444           833333.333
42              4166.667           3385.76389               7552.431           829166.667
43              4166.667           3368.75000               7535.417           825000.000
44              4166.667           3351.73611               7518.403           820833.333
45              4166.667           3334.72222               7501.389           816666.667
46              4166.667           3317.70833               7484.375           812500.000
47              4166.667           3300.69444               7467.361           808333.333
48              4166.667           3283.68056               7450.347           804166.667
49              4166.667           3266.66667               7433.333           800000.000
50              4166.667           3249.65278               7416.319           795833.333
51              4166.667           3232.63889               7399.306           791666.667
52              4166.667           3215.62500               7382.292           787500.000
53              4166.667           3198.61111               7365.278           783333.333
54              4166.667           3181.59722               7348.264           779166.667
55              4166.667           3164.58333               7331.250           775000.000
56              4166.667           3147.56944               7314.236           770833.333
57              4166.667           3130.55556               7297.222           766666.667
58              4166.667           3113.54167               7280.208           762500.000
59              4166.667           3096.52778               7263.194           758333.333
60              4166.667           3079.51389               7246.181           754166.667
61              4166.667           3062.50000               7229.167           750000.000
62              4166.667           3045.48611               7212.153           745833.333
63              4166.667           3028.47222               7195.139           741666.667
64              4166.667           3011.45833               7178.125           737500.000
65              4166.667           2994.44444               7161.111           733333.333
66              4166.667           2977.43056               7144.097           729166.667
67              4166.667           2960.41667               7127.083           725000.000
68              4166.667           2943.40278               7110.069           720833.333
69              4166.667           2926.38889               7093.056           716666.667
70              4166.667           2909.37500               7076.042           712500.000
71              4166.667           2892.36111               7059.028           708333.333
72              4166.667           2875.34722               7042.014           704166.667
73              4166.667           2858.33333               7025.000           700000.000
74              4166.667           2841.31944               7007.986           695833.333
75              4166.667           2824.30556               6990.972           691666.667
76              4166.667           2807.29167               6973.958           687500.000
77              4166.667           2790.27778               6956.944           683333.333
78              4166.667           2773.26389               6939.931           679166.667
79              4166.667           2756.25000               6922.917           675000.000
80              4166.667           2739.23611               6905.903           670833.333
81              4166.667           2722.22222               6888.889           666666.667
82              4166.667           2705.20833               6871.875           662500.000
83              4166.667           2688.19444               6854.861           658333.333
84              4166.667           2671.18056               6837.847           654166.667
85              4166.667           2654.16667               6820.833           650000.000
86              4166.667           2637.15278               6803.819           645833.333
87              4166.667           2620.13889               6786.806           641666.667
88              4166.667           2603.12500               6769.792           637500.000
89              4166.667           2586.11111               6752.778           633333.333
90              4166.667           2569.09722               6735.764           629166.667
91              4166.667           2552.08333               6718.750           625000.000
92              4166.667           2535.06944               6701.736           620833.333
93              4166.667           2518.05556               6684.722           616666.667
94              4166.667           2501.04167               6667.708           612500.000
95              4166.667           2484.02778               6650.694           608333.333
96              4166.667           2467.01389               6633.681           604166.667
97              4166.667           2450.00000               6616.667           600000.000
98              4166.667           2432.98611               6599.653           595833.333
99              4166.667           2415.97222               6582.639           591666.667
100             4166.667           2398.95833               6565.625           587500.000
101             4166.667           2381.94444               6548.611           583333.333
102             4166.667           2364.93056               6531.597           579166.667
103             4166.667           2347.91667               6514.583           575000.000
104             4166.667           2330.90278               6497.569           570833.333
105             4166.667           2313.88889               6480.556           566666.667
106             4166.667           2296.87500               6463.542           562500.000
107             4166.667           2279.86111               6446.528           558333.333
108             4166.667           2262.84722               6429.514           554166.667
109             4166.667           2245.83333               6412.500           550000.000
110             4166.667           2228.81944               6395.486           545833.333
111             4166.667           2211.80556               6378.472           541666.667
112             4166.667           2194.79167               6361.458           537500.000
113             4166.667           2177.77778               6344.444           533333.333
114             4166.667           2160.76389               6327.431           529166.667
115             4166.667           2143.75000               6310.417           525000.000
116             4166.667           2126.73611               6293.403           520833.333
117             4166.667           2109.72222               6276.389           516666.667
118             4166.667           2092.70833               6259.375           512500.000
119             4166.667           2075.69444               6242.361           508333.333
120             4166.667           2058.68056               6225.347           504166.667
121             4166.667           2041.66667               6208.333           500000.000
122             4166.667           2024.65278               6191.319           495833.333
123             4166.667           2007.63889               6174.306           491666.667
124             4166.667           1990.62500               6157.292           487500.000
125             4166.667           1973.61111               6140.278           483333.333
126             4166.667           1956.59722               6123.264           479166.667
127             4166.667           1939.58333               6106.250           475000.000
128             4166.667           1922.56944               6089.236           470833.333
129             4166.667           1905.55556               6072.222           466666.667
130             4166.667           1888.54167               6055.208           462500.000
131             4166.667           1871.52778               6038.194           458333.333
132             4166.667           1854.51389               6021.181           454166.667
133             4166.667           1837.50000               6004.167           450000.000
134             4166.667           1820.48611               5987.153           445833.333
135             4166.667           1803.47222               5970.139           441666.667
136             4166.667           1786.45833               5953.125           437500.000
137             4166.667           1769.44444               5936.111           433333.333
138             4166.667           1752.43056               5919.097           429166.667
139             4166.667           1735.41667               5902.083           425000.000
140             4166.667           1718.40278               5885.069           420833.333
141             4166.667           1701.38889               5868.056           416666.667
142             4166.667           1684.37500               5851.042           412500.000
143             4166.667           1667.36111               5834.028           408333.333
144             4166.667           1650.34722               5817.014           404166.667
145             4166.667           1633.33333               5800.000           400000.000
146             4166.667           1616.31944               5782.986           395833.333
147             4166.667           1599.30556               5765.972           391666.667
148             4166.667           1582.29167               5748.958           387500.000
149             4166.667           1565.27778               5731.944           383333.333
150             4166.667           1548.26389               5714.931           379166.667
151             4166.667           1531.25000               5697.917           375000.000
152             4166.667           1514.23611               5680.903           370833.333
153             4166.667           1497.22222               5663.889           366666.667
154             4166.667           1480.20833               5646.875           362500.000
155             4166.667           1463.19444               5629.861           358333.333
156             4166.667           1446.18056               5612.847           354166.667
157             4166.667           1429.16667               5595.833           350000.000
158             4166.667           1412.15278               5578.819           345833.333
159             4166.667           1395.13889               5561.806           341666.667
160             4166.667           1378.12500               5544.792           337500.000
161             4166.667           1361.11111               5527.778           333333.333
162             4166.667           1344.09722               5510.764           329166.667
163             4166.667           1327.08333               5493.750           325000.000
164             4166.667           1310.06944               5476.736           320833.333
165             4166.667           1293.05556               5459.722           316666.667
166             4166.667           1276.04167               5442.708           312500.000
167             4166.667           1259.02778               5425.694           308333.333
168             4166.667           1242.01389               5408.681           304166.667
169             4166.667           1225.00000               5391.667           300000.000
170             4166.667           1207.98611               5374.653           295833.333
171             4166.667           1190.97222               5357.639           291666.667
172             4166.667           1173.95833               5340.625           287500.000
173             4166.667           1156.94444               5323.611           283333.333
174             4166.667           1139.93056               5306.597           279166.667
175             4166.667           1122.91667               5289.583           275000.000
176             4166.667           1105.90278               5272.569           270833.333
177             4166.667           1088.88889               5255.556           266666.667
178             4166.667           1071.87500               5238.542           262500.000
179             4166.667           1054.86111               5221.528           258333.333
180             4166.667           1037.84722               5204.514           254166.667
181             4166.667           1020.83333               5187.500           250000.000
182             4166.667           1003.81944               5170.486           245833.333
183             4166.667            986.80556               5153.472           241666.667
184             4166.667            969.79167               5136.458           237500.000
185             4166.667            952.77778               5119.444           233333.333
186             4166.667            935.76389               5102.431           229166.667
187             4166.667            918.75000               5085.417           225000.000
188             4166.667            901.73611               5068.403           220833.333
189             4166.667            884.72222               5051.389           216666.667
190             4166.667            867.70833               5034.375           212500.000
191             4166.667            850.69444               5017.361           208333.333
192             4166.667            833.68056               5000.347           204166.667
193             4166.667            816.66667               4983.333           200000.000
194             4166.667            799.65278               4966.319           195833.333
195             4166.667            782.63889               4949.306           191666.667
196             4166.667            765.62500               4932.292           187500.000
197             4166.667            748.61111               4915.278           183333.333
198             4166.667            731.59722               4898.264           179166.667
199             4166.667            714.58333               4881.250           175000.000
200             4166.667            697.56944               4864.236           170833.333
201             4166.667            680.55556               4847.222           166666.667
202             4166.667            663.54167               4830.208           162500.000
203             4166.667            646.52778               4813.194           158333.333
204             4166.667            629.51389               4796.181           154166.667
205             4166.667            612.50000               4779.167           150000.000
206             4166.667            595.48611               4762.153           145833.333
207             4166.667            578.47222               4745.139           141666.667
208             4166.667            561.45833               4728.125           137500.000
209             4166.667            544.44444               4711.111           133333.333
210             4166.667            527.43056               4694.097           129166.667
211             4166.667            510.41667               4677.083           125000.000
212             4166.667            493.40278               4660.069           120833.333
213             4166.667            476.38889               4643.056           116666.667
214             4166.667            459.37500               4626.042           112500.000
215             4166.667            442.36111               4609.028           108333.333
216             4166.667            425.34722               4592.014           104166.667
217             4166.667            408.33333               4575.000           100000.000
218             4166.667            391.31944               4557.986            95833.333
219             4166.667            374.30556               4540.972            91666.667
220             4166.667            357.29167               4523.958            87500.000
221             4166.667            340.27778               4506.944            83333.333
222             4166.667            323.26389               4489.931            79166.667
223             4166.667            306.25000               4472.917            75000.000
224             4166.667            289.23611               4455.903            70833.333
225             4166.667            272.22222               4438.889            66666.667
226             4166.667            255.20833               4421.875            62500.000
227             4166.667            238.19444               4404.861            58333.333
228             4166.667            221.18056               4387.847            54166.667
229             4166.667            204.16667               4370.833            50000.000
230             4166.667            187.15278               4353.819            45833.333
231             4166.667            170.13889               4336.806            41666.667
232             4166.667            153.12500               4319.792            37500.000
233             4166.667            136.11111               4302.778            33333.333
234             4166.667            119.09722               4285.764            29166.667
235             4166.667            102.08333               4268.750            25000.000
236             4166.667             85.06944               4251.736            20833.333
237             4166.667             68.05556               4234.722            16666.667
238             4166.667             51.04167               4217.708            12500.000
239             4166.667             34.02778               4200.694             8333.333
240             4166.667             17.01389               4183.681             4166.667
> 

等额本息:

> # 等额本息
> MPB <- B*R*(1+R)^t / ((1+R)^t-1)   # 每月应还的等额本息,包含月还利息和月还本金
> MYBF <- function(n)      # 如何将利息与本金分离出来
+ {
+   # 可知,MYB(n)为第n个月后剩余的欠银行的钱,即第n个月的剩余本金(n=1,2,3..)
+   # 那么MYB(N),N=0,1,2,...,且MYB(N)*r就是第N+1个月的利息
+   MYB <- B*(1+R)^n - MPB*((1+R)^n-1)/R 
+   return(MYB)
+ }
> MYB <- MYBF(N-1)         # 让时间从0,1,2,3...到n-1结束  #每月剩余本金
> MIB <- MYB * R           # 月还利息
> MBB <- MPB - MIB         # 月还本金
> two <- data.frame('等额本息每月偿还本金'=MBB,'等额本息每月偿还利息'=MIB,'等额本息每月偿还本息和'=MPB,'等额本金每月剩余本金'=MYB)
> two

上述代码没有for循环语句,添加后如下:

> # 等额本息
> MPB <- B*R*(1+R)^t / ((1+R)^t-1)   # 每月应还的等额本息,包含月还利息和月还本金
> two <- data.frame('等额本息每月偿还本金'=0,'等额本息每月偿还利息'=0,'等额本息每月偿还本息和'=0,'等额本息每月剩余本金'=0)
> two
  等额本息每月偿还本金 等额本息每月偿还利息 等额本息每月偿还本息和 等额本息每月剩余本金
1                    0                    0                      0                    0
> MYBF <- function(n)      # 如何将利息与本金分离出来
+ {
+   # 可知,MYB(n)为第n个月后剩余的欠银行的钱,即第n个月的剩余本金(n=1,2,3..)
+   # 那么MYB(N),N=0,1,2,...,且MYB(N)*r就是第N+1个月的利息
+   MYB <- B*(1+R)^n - MPB*((1+R)^n-1)/R 
+   return(MYB)
+ }
> for(i in N-1){
+   MYB <- MYBF(i)         # 让时间从0,1,2,3...到n-1结束  #每月剩余本金
+   MIB <- MYB * R           # 月还利息
+   MBB <- MPB - MIB         # 月还本金
+   two[i+1,] <- data.frame('等额本息每月偿还本金'=MBB,'等额本息每月偿还利息'=MIB,'等额本息每月偿还本息和'=MPB,'等额本金每月剩余本金'=MYB)
+ 
+ }
> two
    等额本息每月偿还本金 等额本息每月偿还利息 等额本息每月偿还本息和 等额本息每月剩余本金
1               2461.107           4083.33333                6544.44          1000000.000
2               2471.157           4073.28381                6544.44           997538.893
3               2481.247           4063.19326                6544.44           995067.736
4               2491.379           4053.06150                6544.44           992586.489
5               2501.552           4042.88837                6544.44           990095.110
......

结果:

等额本息每月偿还本金 等额本息每月偿还利息 等额本息每月偿还本息和 等额本金每月剩余本金
1               2461.107           4083.33333                6544.44          1000000.000
2               2471.157           4073.28381                6544.44           997538.893
3               2481.247           4063.19326                6544.44           995067.736
4               2491.379           4053.06150                6544.44           992586.489
5               2501.552           4042.88837                6544.44           990095.110
6               2511.767           4032.67369                6544.44           987593.558
7               2522.023           4022.41731                6544.44           985081.791
8               2532.321           4012.11905                6544.44           982559.768
9               2542.662           4001.77874                6544.44           980027.446
10              2553.044           3991.39620                6544.44           977484.785
11              2563.469           3980.97127                6544.44           974931.740
12              2573.937           3970.50377                6544.44           972368.271
13              2584.447           3959.99353                6544.44           969794.334
14              2595.000           3949.44037                6544.44           967209.887
15              2605.596           3938.84412                6544.44           964614.887
16              2616.236           3928.20460                6544.44           962009.291
17              2626.919           3917.52164                6544.44           959393.055
18              2637.645           3906.79506                6544.44           956766.136
19              2648.416           3896.02467                6544.44           954128.491
20              2659.230           3885.21031                6544.44           951480.075
21              2670.089           3874.35178                6544.44           948820.845
22              2680.992           3863.44892                6544.44           946150.756
23              2691.939           3852.50154                6544.44           943469.765
24              2702.931           3841.50945                6544.44           940777.826
25              2713.968           3830.47249                6544.44           938074.895
26              2725.050           3819.39045                6544.44           935360.927
27              2736.177           3808.26316                6544.44           932635.877
28              2747.350           3797.09044                6544.44           929899.699
29              2758.568           3785.87209                6544.44           927152.349
30              2769.833           3774.60794                6544.44           924393.781
31              2781.143           3763.29779                6544.44           921623.948
32              2792.499           3751.94146                6544.44           918842.805
33              2803.902           3740.53875                6544.44           916050.306
34              2815.351           3729.08949                6544.44           913246.405
35              2826.847           3717.59347                6544.44           910431.054
36              2838.390           3706.05051                6544.44           907604.207
37              2849.980           3694.46042                6544.44           904765.817
38              2861.617           3682.82300                6544.44           901915.837
39              2873.302           3671.13806                6544.44           899054.219
40              2885.035           3659.40541                6544.44           896180.917
41              2896.816           3647.62485                6544.44           893295.882
42              2908.644           3635.79619                6544.44           890399.066
43              2920.521           3623.91922                6544.44           887490.422
44              2932.447           3611.99376                6544.44           884569.900
45              2944.421           3600.01960                6544.44           881637.454
46              2956.444           3587.99655                6544.44           878693.033
47              2968.516           3575.92440                6544.44           875736.589
48              2980.638           3563.80296                6544.44           872768.073
49              2992.808           3551.63203                6544.44           869787.435
50              3005.029           3539.41139                6544.44           866794.627
51              3017.300           3527.14086                6544.44           863789.598
52              3029.620           3514.82022                6544.44           860772.298
53              3041.991           3502.44927                6544.44           857742.678
54              3054.413           3490.02780                6544.44           854700.687
55              3066.885           3477.55562                6544.44           851646.274
56              3079.408           3465.03251                6544.44           848579.389
57              3091.982           3452.45826                6544.44           845499.981
58              3104.608           3439.83266                6544.44           842407.999
59              3117.285           3427.15551                6544.44           839303.391
60              3130.014           3414.42660                6544.44           836186.106
61              3142.795           3401.64571                6544.44           833056.092
62              3155.628           3388.81263                6544.44           829913.297
63              3168.513           3375.92715                6544.44           826757.669
64              3181.451           3362.98905                6544.44           823589.156
65              3194.442           3349.99813                6544.44           820407.705
66              3207.486           3336.95415                6544.44           817213.262
67              3220.584           3323.85692                6544.44           814005.776
68              3233.734           3310.70620                6544.44           810785.192
69              3246.939           3297.50179                6544.44           807551.458
70              3260.197           3284.24345                6544.44           804304.519
71              3273.510           3270.93098                6544.44           801044.322
72              3286.876           3257.56415                6544.44           797770.813
73              3300.298           3244.14274                6544.44           794483.937
74              3313.774           3230.66653                6544.44           791183.639
75              3327.305           3217.13528                6544.44           787869.865
76              3340.892           3203.54879                6544.44           784542.560
77              3354.534           3189.90681                6544.44           781201.668
78              3368.231           3176.20913                6544.44           777847.134
79              3381.985           3162.45552                6544.44           774478.903
80              3395.795           3148.64575                6544.44           771096.918
81              3409.661           3134.77959                6544.44           767701.123
82              3423.584           3120.85680                6544.44           764291.462
83              3437.563           3106.87717                6544.44           760867.879
84              3451.600           3092.84045                6544.44           757430.315
85              3465.694           3078.74642                6544.44           753978.715
86              3479.846           3064.59484                6544.44           750513.021
87              3494.055           3050.38547                6544.44           747033.176
88              3508.322           3036.11808                6544.44           743539.120
89              3522.648           3021.79243                6544.44           740030.798
90              3537.032           3007.40828                6544.44           736508.150
91              3551.475           2992.96540                6544.44           732971.118
92              3565.977           2978.46354                6544.44           729419.643
93              3580.538           2963.90247                6544.44           725853.666
94              3595.159           2949.28194                6544.44           722273.128
95              3609.839           2934.60171                6544.44           718677.969
96              3624.579           2919.86153                6544.44           715068.130
97              3639.379           2905.06117                6544.44           711443.551
98              3654.240           2890.20037                6544.44           707804.172
99              3669.162           2875.27889                6544.44           704149.932
100             3684.144           2860.29648                6544.44           700480.770
101             3699.188           2845.25289                6544.44           696796.626
102             3714.293           2830.14788                6544.44           693097.439
103             3729.459           2814.98118                6544.44           689383.146
104             3744.688           2799.75255                6544.44           685653.687
105             3759.979           2784.46175                6544.44           681908.999
106             3775.332           2769.10850                6544.44           678149.020
107             3790.748           2753.69256                6544.44           674373.688
108             3806.227           2738.21367                6544.44           670582.940
109             3821.769           2722.67158                6544.44           666776.713
110             3837.374           2707.06602                6544.44           662954.945
111             3853.044           2691.39674                6544.44           659117.570
112             3868.777           2675.66348                6544.44           655264.526
113             3884.575           2659.86598                6544.44           651395.749
114             3900.437           2644.00396                6544.44           647511.175
115             3916.363           2628.07718                6544.44           643610.738
116             3932.355           2612.08536                6544.44           639694.375
117             3948.412           2596.02825                6544.44           635762.020
118             3964.535           2579.90556                6544.44           631813.608
119             3980.723           2563.71705                6544.44           627849.073
120             3996.978           2547.46243                6544.44           623868.349
121             4013.299           2531.14143                6544.44           619871.371
122             4029.687           2514.75379                6544.44           615858.072
123             4046.141           2498.29924                6544.44           611828.385
124             4062.663           2481.77750                6544.44           607782.244
125             4079.252           2465.18829                6544.44           603719.581
126             4095.909           2448.53134                6544.44           599640.329
127             4112.634           2431.80638                6544.44           595544.420
128             4129.427           2415.01313                6544.44           591431.786
129             4146.289           2398.15130                6544.44           587302.358
130             4163.220           2381.22062                6544.44           583156.069
131             4180.220           2364.22080                6544.44           578992.849
132             4197.289           2347.15157                6544.44           574812.630
133             4214.428           2330.01264                6544.44           570615.341
134             4231.637           2312.80373                6544.44           566400.913
135             4248.916           2295.52454                6544.44           562169.276
136             4266.266           2278.17480                6544.44           557920.360
137             4283.686           2260.75422                6544.44           553654.094
138             4301.178           2243.26250                6544.44           549370.408
139             4318.741           2225.69936                6544.44           545069.230
140             4336.376           2208.06450                6544.44           540750.489
141             4354.083           2190.35763                6544.44           536414.113
142             4371.862           2172.57846                6544.44           532060.030
143             4389.714           2154.72669                6544.44           527688.168
144             4407.638           2136.80202                6544.44           523298.454
145             4425.636           2118.80416                6544.44           518890.816
146             4443.708           2100.73282                6544.44           514465.180
147             4461.853           2082.58768                6544.44           510021.472
148             4480.072           2064.36844                6544.44           505559.619
149             4498.366           2046.07482                6544.44           501079.547
150             4516.734           2027.70649                6544.44           496581.181
151             4535.177           2009.26316                6544.44           492064.447
152             4553.696           1990.74452                6544.44           487529.270
153             4572.290           1972.15026                6544.44           482975.574
154             4590.960           1953.48008                6544.44           478403.284
155             4609.707           1934.73365                6544.44           473812.323
156             4628.530           1915.91068                6544.44           469202.617
157             4647.430           1897.01085                6544.44           464574.087
158             4666.407           1878.03385                6544.44           459926.657
159             4685.461           1858.97936                6544.44           455260.251
160             4704.593           1839.84706                6544.44           450574.789
161             4723.804           1820.63663                6544.44           445870.196
162             4743.093           1801.34777                6544.44           441146.392
163             4762.460           1781.98014                6544.44           436403.299
164             4781.907           1762.53343                6544.44           431640.839
165             4801.433           1743.00731                6544.44           426858.932
166             4821.039           1723.40145                6544.44           422057.499
167             4840.725           1703.71554                6544.44           417236.460
168             4860.491           1683.94925                6544.44           412395.735
169             4880.338           1664.10224                6544.44           407535.244
170             4900.266           1644.17420                6544.44           402654.905
171             4920.276           1624.16478                6544.44           397754.639
172             4940.367           1604.07365                6544.44           392834.363
173             4960.540           1583.90049                6544.44           387893.996
174             4980.796           1563.64495                6544.44           382933.456
175             5001.134           1543.30670                6544.44           377952.661
176             5021.555           1522.88540                6544.44           372951.527
177             5042.060           1502.38072                6544.44           367929.972
178             5062.648           1481.79231                6544.44           362887.912
179             5083.321           1461.11983                6544.44           357825.264
180             5104.078           1440.36294                6544.44           352741.943
181             5124.919           1419.52129                6544.44           347637.866
182             5145.846           1398.59453                6544.44           342512.947
183             5166.858           1377.58233                6544.44           337367.101
184             5187.956           1356.48432                6544.44           332200.243
185             5209.140           1335.30017                6544.44           327012.286
186             5230.411           1314.02951                6544.44           321803.146
187             5251.768           1292.67200                6544.44           316572.735
188             5273.213           1271.22728                6544.44           311320.967
189             5294.745           1249.69499                6544.44           306047.753
190             5316.366           1228.07478                6544.44           300753.008
191             5338.074           1206.36629                6544.44           295436.642
192             5359.871           1184.56915                6544.44           290098.568
193             5381.757           1162.68301                6544.44           284738.697
194             5403.733           1140.70750                6544.44           279356.939
195             5425.798           1118.64226                6544.44           273953.206
196             5447.954           1096.48692                6544.44           268527.408
197             5470.199           1074.24111                6544.44           263079.454
198             5492.536           1051.90446                6544.44           257609.255
199             5514.964           1029.47660                6544.44           252116.719
200             5537.483           1006.95717                6544.44           246601.755
201             5560.095            984.34578                6544.44           241064.272
202             5582.798            961.64206                6544.44           235504.177
203             5605.595            938.84563                6544.44           229921.379
204             5628.484            915.95612                6544.44           224315.784
205             5651.467            892.97314                6544.44           218687.299
206             5674.544            869.89631                6544.44           213035.832
207             5697.715            846.72526                6544.44           207361.288
208             5720.981            823.45959                6544.44           201663.573
209             5744.342            800.09892                6544.44           195942.592
210             5767.798            776.64285                6544.44           190198.250
211             5791.349            753.09101                6544.44           184430.452
212             5814.997            729.44300                6544.44           178639.103
213             5838.742            705.69843                6544.44           172824.106
214             5862.584            681.85690                6544.44           166985.363
215             5886.522            657.91802                6544.44           161122.780
216             5910.559            633.88138                6544.44           155236.257
217             5934.694            609.74660                6544.44           149325.698
218             5958.927            585.51327                6544.44           143391.004
219             5983.260            561.18098                6544.44           137432.077
220             6007.691            536.74934                6544.44           131448.818
221             6032.223            512.21793                6544.44           125441.127
222             6056.854            487.58636                6544.44           119408.904
223             6081.586            462.85420                6544.44           113352.050
224             6106.419            438.02106                6544.44           107270.464
225             6131.354            413.08651                6544.44           101164.044
226             6156.390            388.05015                6544.44            95032.690
227             6181.529            362.91156                6544.44            88876.300
228             6206.770            337.67031                6544.44            82694.771
229             6232.114            312.32600                6544.44            76488.001
230             6257.562            286.87820                6544.44            70255.886
231             6283.114            261.32649                6544.44            63998.324
232             6308.770            235.67044                6544.44            57715.210
233             6334.531            209.90963                6544.44            51406.440
234             6360.397            184.04363                6544.44            45071.909
235             6386.368            158.07201                6544.44            38711.512
236             6412.446            131.99434                6544.44            32325.144
237             6438.630            105.81018                6544.44            25912.698
238             6464.921             79.51911                6544.44            19474.067
239             6491.320             53.12068                6544.44            13009.146
240             6517.826             26.61446                6544.44             6517.826
> 

忘记计算利息差额:
添加入下:
完整代码:

#输入数据
B <- 1000000
R <- 0.049/12 #月利率
t <- 20*12 #分t期还款
N <- c(1:t)
one <- data.frame('等额本金每月偿还本金'=0,'等额本金每月偿还利息'=0,'等额本金每月偿还本息和'=0,'等额本金每月剩余本金'=0)
one

#等额本金
a=0#计算利息总和
MBA <- B/t #每月偿还本金=贷款总额/贷款月数
for(i in N){
  MYA <- B - MBA*(i-1) #每月剩余本金
  MIA <- MYA*R #每月偿还利息=剩余本金*月利率
  a=a+MIA
  MPA <- MIA + MBA #每月偿还本息和
  one[i,] <- data.frame('等额本金每月偿还本金'=MBA,'等额本金每月偿还利息'=MIA,'等额本金每月偿还本息和'=MPA,'等额本金每月剩余本金'=MYA)
}
one
a

# 等额本息
MPB <- B*R*(1+R)^t / ((1+R)^t-1)   # 每月应还的等额本息,包含月还利息和月还本金
two <- data.frame('等额本息每月偿还本金'=0,'等额本息每月偿还利息'=0,'等额本息每月偿还本息和'=0,'等额本息每月剩余本金'=0)
two
MYBF <- function(n)      # 如何将利息与本金分离出来
{
  # 可知,MYB(n)为第n个月后剩余的欠银行的钱,即第n个月的剩余本金(n=1,2,3..)
  # 那么MYB(N),N=0,1,2,...,且MYB(N)*r就是第N+1个月的利息
  MYB <- B*(1+R)^n - MPB*((1+R)^n-1)/R 
  return(MYB)
}

b=0#计算利息总和
for(i in N-1){
  MYB <- MYBF(i)         # 让时间从0,1,2,3...到n-1结束  #每月剩余本金
  MIB <- MYB * R           # 月还利息
  b=b+MIB
  MBB <- MPB - MIB         # 月还本金
  two[i+1,] <- data.frame('等额本息每月偿还本金'=MBB,'等额本息每月偿还利息'=MIB,'等额本息每月偿还本息和'=MPB,'等额本金每月剩余本金'=MYB)

}
two
b
#等额本息偿还利息总和-等额本金偿还利息总和
c=b-a
print("等额本息偿还利息总和-等额本金偿还利息总和")
c

等额本息偿还利息总和-等额本金偿还利息总和-----运行结果

> a
[1] 492041.7
> b
[1] 570665.7


> #等额本息偿还利息总和-等额本金偿还利息总和
> c=b-a
> print("等额本息偿还利息总和-等额本金偿还利息总和")
[1] "等额本息偿还利息总和-等额本金偿还利息总和"
> c
[1] 78624.05

你可能感兴趣的:(R语言小代码)