I have written a function for amortising a loan given the loan characteristics and for providing me with the present value of the payment cash flows:
mortgage<-function(P,I,L,amort,periodicity,n,residual,discount){
if(periodicity<-TRUE){i<-I/n}else{i<-I}
N<-n*L
payment<- -((residual-(P*((1+i)^N)))/((((1+i)^N)-1)/i))
pay<<-payment
if(amort==T){
Pt<-P
currP<-NULL
while(Pt>=residual){
H<-Pt*i
C<- payment-H
Q<-Pt-C
Pt<-Q
currP<-c(currP,Pt)}
monthP<-c(P,currP[1:(length(currP)-1)])-currP
adFmonth<<-data.frame(
Amortization=c(P,currP[1:(length(currP)-1)]),
Payment<-monthP+c((pay-monthP)[1:(length(monthP)-1)],0),
Principal=monthP,
Interest=c((pay-monthP)[1:(length(monthP)-1)],0),
Time=sort(rep(1:ceiling(L*n/n),n))[1:length(monthP)]
)
adFmonth<<-adFmonth
}
AssetValue<-pv(discount/n,N,residual,pay)
return(AssetValue)
}
Given a set of (different) loans and a matrix containing the value of each function argument per loan, I would now like to create a loop, as that the function loops through each column of the matrix and gives me the output of the function.
Can anyone help me with that endeavour?
Thank you very much.