mercredi 25 novembre 2020

If loop - Argument is of length zero - error

I do have the following code for a loop, which basically only updates the column "decay rate" based on whether or not the column "RPK_cap" is higher than a certain threshold which is exogenously given(in scenario 1 it is 1200, in Scenario 2 it is 1300 etc). If that is the case than I want to decrease the decay rate by multiplying it my 0.9. This is done for all countries (column: iso) and years(column: year) which are in the data.table. When I am using scenario 1 data the column "RPK_cap" will be filled with scenario 1 data and if I choose scenario 2 the column "RPK_cap" will be filled with scenario 2 data respectively.

And my problem is the following: This loop works ONLY for scenario 1. If I am using scenario 2 I get the error message: "Error in if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == : argument is of length zero"

I really tried a lot and I just don´t know what the problem is here. Does anyone know what the problem is?

  if (scenario == "1") {
    for (j in unique(price_el_int_aviation_RPK$iso)) {
      for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) { 
        if (price_el_int_aviation_RPK$RPK_cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1200) { 
          price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.90  
        }
      }
    }
  } else if (scenario == "2"){ 
    for (j in unique(price_el_int_aviation_RPK$iso)) {
      for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) { 
        if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1300) { 
          price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.95
        }
      }
    }
  } else if (scenario == "3"){ 
    for (j in unique(price_el_int_aviation_RPK$iso)) {
      for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) { 
        if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1400) { 
          price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.95
        }
      }
    }
  } else if (scenario == "4"){ 
    for (j in unique(price_el_int_aviation_RPK$iso)) {
      for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) { 
        if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1500) { 
          price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.95
        }
      }
    }
  } else if (scenario == "5"){ 
    for (j in unique(price_el_int_aviation_RPK$iso)) {
      for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) { 
        if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1600) { 
          price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.99
        }
      }
    }
  }else{}
'''

Aucun commentaire:

Enregistrer un commentaire