title: Error in if condition {: argument is of length zero
body: <p>I have a R dataframe <code>df</code> as given below:</p>
  
  <pre><code>ID        End_week
  2500076   1223
  6801102   1325
  6801282   1308
  6803252   1426
  9882106   1426
  9894112   1217
  </code></pre>
  
  <p>I want to insert a new column <code>status</code> with 0/1 values - 1 if End_week equals 1426 and 0 otherwise. Final output should look like this:</p>
  
  <pre><code>ID        End_week   Status
  2500076   1223       0
  6801102   1325       0
  6801282   1308       0
  6803252   1426       1
  9882106   1426       1
  9894112   1217       0
  </code></pre>
  
  <p>I wrote a while loop as given below:</p>
  
  <pre><code>while(i&lt;=length(df$End_week)) {
    if(df$End_week[i]==1426) {
      status[i] &lt;- 1
    } else {
      status[i] &lt;- 0
    }
    i=i+1
  }
  </code></pre>
  
  <p>But, I get the following error:</p>
  
  <pre><code>Error in if (df$End_week[i] == 1426) { : 
    argument is of length zero
  </code></pre>
  
  <p>I know that the question has been asked before and so I <a href="https://stackoverflow.com/questions/27350636/r-argument-is-of-length-zero-in-if-statement">referred</a> <a href="https://stackoverflow.com/questions/12307139/error-in-if-while-condition-argument-is-of-length-zero">these</a> <a href="https://stackoverflow.com/questions/27015180/error-in-if-argument-is-of-length-zero-in-r">answers</a>, but didn't find them helpful in my scenario. I checked for any <code>NA</code> values in <code>End_week</code> column and there aren't any. Can someone suggest a way out?</p>