-1

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? This is the data.table data.table_example

  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{}
  • 3
    Your example is not reproducible and it is not minimal. Please read [mre]! – jogo Nov 25 '20 at 12:32
  • 1
    Please read [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269/4996248) for what a [mcve] would look like in R. It is a fair amount of work to write a good question, but it is worth it. For one thing, you are more likely to get a good answer. For another, you learn good debugging skills when you make such an effort. – John Coleman Nov 25 '20 at 12:36
  • 1
    To clarify the above comments, providing your data as a screenshot does not help us help you because we cannot use the screenshot to reproduce your error on our own system. You will need to use something like `dput(head(my data))` and paste the output into your question, so we can copy that code and run your loop. – qdread Nov 25 '20 at 12:40

1 Answers1

0

It's hard to answer in specifics because we cannot see your data.

But the error you see

 "Error in if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == : argument is of length zero"

is because price_el_int_aviation_RPK$iso is of length zero - it's a vector without anything in it!

Here's a minimal example of the problem

# Create a vector of length zero
a <- c()

# Prove it's length is zero
length(a)
# [1] 0

# This is a minimal example of what's going wrong with scenario 2 in your script
if(a == 2) { print("it works") }
Error in if (a == 2) if (a == 2) { : argument is of length zero
stevec
  • 41,291
  • 27
  • 223
  • 311
  • Yes but when im am using length(price_el_int_aviation_RPK$iso) the length is not 0 its 9624. I think I just identified the problem: I have to interpolate some values of the column "RPK_Cap" with by using the function na.locf(x). And somehow the loop does not work with these data. But the weird thing is it does work for scenario 1 for which i also interpolate these values. – Sebastian.DataAnalyst Nov 25 '20 at 13:48
  • 1
    @Sebastian.DataAnalyst when debugging loops, immediately after an error, I type the iterator into the console (e.g. if I had `for(i in 1:10) ...` then I simply type `i` and press enter, and see precisely which iteration it got to. Then I ignore the loop, but go the the first line of code within the loop, and run it, then the second, then the third, and so on, until I find the line erroring. Then inspect that line of code and it's parts very closely until you find what's wrong with it – stevec Nov 25 '20 at 13:50