R中的递归循环(使用tidyverse)

时间:2019-04-08 14:50:31

标签: r tidyverse purrr

我正在尝试使用tidyverse而不是编写循环来实现简单的摊销时间表。我假设有一种使用tidyverse的方法。完成这项工作最接近的帮助是

我要解决的问题如下

For Month 1, 
LoanRem = Loan
Beyond Month 1
LoanRem = Loan - Cumulative_Sum_Previous(Principal_Payments)

PrinPay[i] = A + ExtraPrinPay[i] - Int * ( Loan  - (PrinPay[1] + .. PrinPay[i-1] )

n = <constant> length of the payment scheme

A(每月付款),Int(每月利率),Loan(初始贷款金额)都是常数

ExtraPrinPay是长度为n的向量。

我得到帮助的最近的链接可以解决一个稍微不同的问题,该链接Recursive function using dplyr

Cost <- 200000;
Down <- 0.2 * Cost;
Loan <- Cost - Down;
Int  <- 0.04 / 12;
TermY<- 6;
Term <- TermY;
A    <- Loan * Int * ( 1 + Int )^(Term) / ( ( 1 + Int )^(Term) - 1.0 );

LoanRem <- function(x,y,Epp,A=A,Int=Int,Loan=Loan) {
  return(A + Epp - Int * Loan - int * x);
}
# LoanRem = if_else(Mon==1,Loan,Loan-lag(LiqGain,1)),
# PrinPay = A + Curr(ExtraPrinPay) - Int * Loan - int * cumsum_1(PrinPay),
data <- data.frame(Mon=seq(1,6,1),ExtraPrinPay=0) %>%
        mutate(PrinPay=accumulate2(PrinPay,,Epp=ExtraPrinPay,.f=LoanRem,.init = 0))

我得到了错误

Error in mutate_impl(.data, dots) : 
  Evaluation error: object 'PrinPay' not found.
In addition: Warning message:
package ‘bindrcpp’ was built under R version 3.4.4 

有人可以帮我吗

0 个答案:

没有答案