title: Where is my error (R Programming)
body: <p>The below code works <code>when i=0</code> and having problem <code>when i=2</code>. <br>Below is the error:</p>
  
  <blockquote>
    <p>"Error in while (Net_Present_value$NPV[year] &lt;= 0) { : 
      missing value where TRUE/FALSE needed" in second iteration</p>
  </blockquote>
  
  <pre><code>    vaaa=10000
      concession&lt;-data.frame("case1"=integer(vaaa),"case2"=integer(vaaa),"case3"=integer(vaaa))
  
      #discount
      discount&lt;-c(10,11,12)
      discount_sd&lt;-c(2,2.5,2)
      #volume
      volume_growth&lt;-c(15,16,17)
      volume_growth_sd&lt;-c(2.5,2,3)
      #Cost
      C_cost&lt;-c(-3000000,-3500000,-4000000) # Construction cost in dollar
      volume&lt;-c(600000,700000,800000) #quantity of licence to be converted to smart driving licence
      #cost per unit
      cost_to_produce&lt;-c(8,7,8) #including all cost such as material, operation and maintenance
      cost_to_driver&lt;c(11,12,13)
  
      Net_Present_value&lt;-data.frame(year=integer(25),value=double(25),NPV=double(25))
      cash_flow&lt;-data.frame(year=integer(25),value=double(25))
  
      for(i in 1:3){
        bstrap_discount &lt;- rnorm(vaaa,discount[i],discount_sd[i])
        #Volume
        bstrap_volume &lt;- rnorm(vaaa,volume_growth[i],volume_growth_sd[i])
  
        for( iteration in 1:vaaa){
          year&lt;-1
          #Net_Present_value=0
          #cash_flow=0
  
          Net_Present_value$NPV[year]&lt;-C_cost[i]
          cash_flow$value[year]&lt;-C_cost[i]
          new_volume&lt;-volume[i]
          print(iteration)
  
          while(Net_Present_value$NPV[year]&lt;=0){
            #print(Net_Present_value$NPV[year])
            year&lt;-year+1
  
            if(year==2){ #everybody should change old to new driving license
              Revenue&lt;-volume[i]*(cost_to_driver[i]-cost_to_produce[i])
              NPV&lt;-Revenue/(1+bstrap_discount[iteration]/100)^(year)
            }
  
            else if(year&gt;2){
              temp_vol&lt;-new_volume
              new_volume&lt;-temp_vol*bstrap_volume[iteration]/100+temp_vol
              Revenue&lt;-(new_volume-temp_vol)*(cost_to_driver[i]-cost_to_produce[i])
              NPV&lt;-Revenue/(1+bstrap_discount[iteration]/100)^(year)
            }
  
            Net_Present_value$year[year]&lt;-year
            Net_Present_value$NPV[year]=Net_Present_value$NPV[year-1]+NPV
  
            cash_flow$year[year]&lt;-year
  
            cash_flow$value[year]&lt;-Revenue
  
          }
  
          concession$count[iteration]&lt;-iteration
          if(i==1)
            concession$case1[iteration]&lt;-year
          else if (i==2)
            concession$case2[iteration]&lt;-year
          else if (i==3)
            concession$case3[iteration]&lt;-year
          } 
  }
  </code></pre>