title: R “argument is of length zero” in if(condition)
body: <p>Very new to R, would appreciate if you could tell me what I’m doing wrong. </p>
  
  <p>This was my initial code:</p>
  
  <pre><code>count &lt;- 0
  for (i in 1:nrow(data))
  {
      if(outcome[i]==data[1,i])
      inc(count) &lt;- 1  #package Hmisc
  }
  </code></pre>
  
  <p>where outcome and data[1] are vectors with binary values eg. [1, 0, 0, 1, 1, 0..]</p>
  
  <p>I got the result ‘argument is of length zero’ with the if statement. Tested it individually with rows that matched/mismatched, discovered that the result shows up only in the latter. </p>
  
  <p>So I amended my code to:</p>
  
  <pre><code>count &lt;- 0
  for (i in 1:nrow(data))
  {
      if(!is.null(outcome[i]==data[1,i]))
      inc(count) &lt;- 1  #package Hmisc
  }
  </code></pre>
  
  <p>Now the value that count returned is the number of ‘for' iterations.</p>
  
  <p>Doesn’t this mean R is NOT returning a null value for ANY mismatch in the new code?? How is this happening? Solutions please?</p>