我执行了摊销表功能(每笔贷款的amort.table)
。
amort.table(Loan = 250000,n=48,i=0.205,pf = 12)
我用于每笔贷款的特定代码就是这个:
#Convert payments from class numeric to class integer and loan identifiers to character
prestamos$Amount_of_Payments <- as.integer(prestamos$Amount_of_Payments)
prestamos$NO_CREDITO <- as.character(prestamos$NO_CREDITO)
#A unique list of the loans
serie_unica <- unique(prestamos$NO_CREDITO)
#Calculate the amortization table for each loan
amortizacion <- function(i) {
prestamo_especifico <- prestamos %>%
filter(NO_CREDITO == serie_unica[i])
amort_results <- amort.table(Loan = prestamo_especifico$Amount,
n = prestamo_especifico$Amount_of_Payments,
i = prestamo_especifico$Interest_rate,
pf = 12) #all the results
Results <- as.data.frame(amort_results$Schedule) #getting a data frame from the desired results
Results$NO_CREDITO <- serie_unica #adding an identifier
Results$Starting_date <- ifelse(Results$Year == 0.08, prestamo_especifico$Starting_date, NA) #loan starting date
Results$Amount_of_Payments <- ifelse(Results$Year == 0.08, prestamo_especifico$Amount_of_Payments, NA)
Results
}
amortization_table <- lapply(1:length(serie_unica), amortizacion) %>%
bind_rows() #joining all the data
我想要填写每个NO_CREDITO
的日期,但是我的代码无法正常工作。您可以避免看这件事,并以自己的方式来做:
amortization_table <- amortization_table %>%
mutate_at(vars(ends_with("Date")), as.Date, "%d/%m/%y") %>%
mutate(Fecha = Starting_Date) %>%
group_by(NO_CREDITO) %>%
complete(Fecha = seq(Fecha, lenght.out = NO_CREDITO, by= "1 month")) %>%
mutate(Month = row_number())
这是我的数据的子集:
structure(list(NO_CREDITO = c(2652L, 14829L, 78481L, 110531L,
21796L, 32273L), Amount = c(50000, 250000, 135043.52, 31625.28,
15274.58, 38195.28), Interest_rate = c(0.095, 0.2, 0.23, 0.091332,
0.108953, 0.104545), Starting_date = structure(c(1071460800,
1235449800, 1417753800, 1478577600, 1310617800, 1340253000), class = c("POSIXct",
"POSIXt"), tzone = ""), Loan_length_in_years = c(0.5, 4, 0.5,
1, 1, 1), Amount_of_Payments = c(6L, 48L, 6L, 12L, 12L, 12L)), row.names = c(NA,
6L), class = "data.frame")
现在,假设Loan_length
以年为单位,您必须将它们转换为月,然后将贷款总额除以月数,因此:
Capital <- (df$Amount * (1 + df$Interest_rate / 100)) / (df$Loan_lenght * 12)
同样,为了查看贷款结束日期,请将相应的时间量添加到开始日期。
library(lubridate) End <- df$Starting_date month(End) <- month(End) + df$Loan_length * 12
这里,由于df
中的所有列的长度均为6
,所以Capital
的长度也为6
,每个条目都对应于其ID
值。
cbind(df$ID, Capital, df$Starting_date, End)