如果在R语言中使用else语句,则在“}”中出现意外错误'}]

时间:2017-03-20 12:23:20

标签: r if-statement

我正在计算数据挖掘主题中的债券价值示例。我有if else功能的问题。这是......

P<-1000 #original value - Menh gia trai phieu
T<-20 #maturity - Thoi han trai phieu - semi
r<-.06 #annual rate - Lai suat yeu cau
C<-30 #present value of coupons - Lai dinh ki

BV <- function(P,C,r,t,T){ #t = -th year
    #Find coupon Bon Value at time t mat T - Gia trai phieu
    tmat <- T-t # How many years have you get your bonds for?
    acrued <- C*2*t #already paid
    if(tmat != 0) { #include interim coupons
        i <- seq(1,2*tmat) #BVsemi - seq()=for loop
        acrued + sum(C/(1+r/2)^i) + P/(1+r/2)^(2*tmat)
    }else{ 
        acrued + P/(1+r/2)^(2*tmat) 
    }
}

错误消息是: **

> }
Error: unexpected '}' in "}"

**

我已经阅读了与我的问题相关的所有主题,但没有一个能够很好地运作...... 请告诉我这个问题的亮点......

谢谢

Hoang Anh

1 个答案:

答案 0 :(得分:0)

问题没有复制。我对你的代码进行了复制,并没有收到任何错误。

> P<-1000 #original value - Menh gia trai phieu
> T<-20 #maturity - Thoi han trai phieu - semi
> r<-.06 #annual rate - Lai suat yeu cau
> C<-30 #present value of coupons - Lai dinh ki
> 
> BV <- function(P,C,r,t,T){ #t = -th year
+     #Find coupon Bon Value at time t mat T - Gia trai phieu
+     tmat <- T-t # How many years have you get your bonds for?
+     acrued <- C*2*t #already paid
+     if(tmat != 0) { #include interim coupons
+         i <- seq(1,2*tmat) #BVsemi - seq()=for loop
+         acrued + sum(C/(1+r/2)^i) + P/(1+r/2)^(2*tmat)
+     }else{ 
+         acrued + P/(1+r/2)^(2*tmat) 
+     }
+ }
>