【问题标题】:argument "term" is missing, with no default ,when creating a function创建函数时缺少参数“term”,没有默认值
【发布时间】:2018-04-18 05:11:10
【问题描述】:

我想计算数据集在估值日期的未偿还贷款。我定义了一个函数,但是当我调用该函数时,它给出了一个错误,说第五个参数丢失(term)。

功能如下:

loan_outstanding<-function(c_date,v_date,l_amt,int ,pmt,term){
  l_amt<-as.numeric(l_amt)
  freq<-12
  term_in_months<-as.numeric(term)*freq

    c_date<-as.Date(c_date,"%d/%m/%Y")
    Interest_Rate<-per_to_num(int)

  v_date<-as.Date(v_date,"%Y-%m-%d")
  date_<-numeric(500)
  date_[1]<-as.character(c_date)
  int_cal<-numeric(500)
  cap_repay<-numeric(500)
  loan_out<-numeric(500)
  loan_out[1]<-l_amt
  i<-2
  while(as.Date(date_[i-1],"%Y-%m-%d")<v_date){
    date_[i]<-as.character(AddMonths(as.Date(date_[i-1],"%Y-%m-%d"),1),"%Y-%m-%d")
    int_cal[i]<-loan_out[i-1]*((1+Interest_Rate)^(1/freq)-1)      
    cap_repay[i]<-pmt-int_cal[i]  
    loan_out[i]<-max(loan_out[i-1]-cap_repay[i],0)
    i<-i+1
    }
  val<- loan_out[i-2]
  return(val)
  }

错误:

>loan_outstanding("28/07/2011","2017-03-31",500000,7,9629.918)
 argument "term" is missing, with no default

我的代码有错误吗?

【问题讨论】:

  • 您在尝试调用函数时指定了 5 个参数,但定义的函数有 6 个。错误告诉您 - 您没有指定“术语”并且它没有默认值值集。
  • 谢谢!!! @thelatemail
  • 为了更容易看到发生了什么,在第一个或两个参数(甚至全部)之后命名参数通常是个好主意loan_outstanding("28/07/2011","2017-03-31",l_amt=500000,int =7,pmt=9629.918,term = ???)

标签: r function arguments


【解决方案1】:

这不是一个很好的解决方案,但我只是创建了另一个变量并将其放在函数定义中的最后一个变量之后:

...pmt, term, [new var]) {

它为我解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2016-06-08
    • 2017-12-09
    相关资源
    最近更新 更多