以下代码有效when i=0
,问题when i=2
。
以下是错误:
" while时出错(Net_Present_value $ NPV [year]< = 0){: 缺少需要TRUE / FALSE的值"在第二次迭代中
vaaa=10000
concession<-data.frame("case1"=integer(vaaa),"case2"=integer(vaaa),"case3"=integer(vaaa))
#discount
discount<-c(10,11,12)
discount_sd<-c(2,2.5,2)
#volume
volume_growth<-c(15,16,17)
volume_growth_sd<-c(2.5,2,3)
#Cost
C_cost<-c(-3000000,-3500000,-4000000) # Construction cost in dollar
volume<-c(600000,700000,800000) #quantity of licence to be converted to smart driving licence
#cost per unit
cost_to_produce<-c(8,7,8) #including all cost such as material, operation and maintenance
cost_to_driver<c(11,12,13)
Net_Present_value<-data.frame(year=integer(25),value=double(25),NPV=double(25))
cash_flow<-data.frame(year=integer(25),value=double(25))
for(i in 1:3){
bstrap_discount <- rnorm(vaaa,discount[i],discount_sd[i])
#Volume
bstrap_volume <- rnorm(vaaa,volume_growth[i],volume_growth_sd[i])
for( iteration in 1:vaaa){
year<-1
#Net_Present_value=0
#cash_flow=0
Net_Present_value$NPV[year]<-C_cost[i]
cash_flow$value[year]<-C_cost[i]
new_volume<-volume[i]
print(iteration)
while(Net_Present_value$NPV[year]<=0){
#print(Net_Present_value$NPV[year])
year<-year+1
if(year==2){ #everybody should change old to new driving license
Revenue<-volume[i]*(cost_to_driver[i]-cost_to_produce[i])
NPV<-Revenue/(1+bstrap_discount[iteration]/100)^(year)
}
else if(year>2){
temp_vol<-new_volume
new_volume<-temp_vol*bstrap_volume[iteration]/100+temp_vol
Revenue<-(new_volume-temp_vol)*(cost_to_driver[i]-cost_to_produce[i])
NPV<-Revenue/(1+bstrap_discount[iteration]/100)^(year)
}
Net_Present_value$year[year]<-year
Net_Present_value$NPV[year]=Net_Present_value$NPV[year-1]+NPV
cash_flow$year[year]<-year
cash_flow$value[year]<-Revenue
}
concession$count[iteration]<-iteration
if(i==1)
concession$case1[iteration]<-year
else if (i==2)
concession$case2[iteration]<-year
else if (i==3)
concession$case3[iteration]<-year
}
}