title: for if/elseif loop not working in R
body: <p>I want to calculate the sum of the following logic below. Basically add one to numTrade whenever the number switches from 0 to -1 or 1, and also when number switches between 1 and -1. Treat consecutive elements as 0, such that (0, 1, 1, 1) is counted as 1 and (1,0,-1,1) is counted as 2.</p>
  
  <p>numTrade should return total of 4, but instead it returns 3. Can someone please explain why? </p>
  
  <pre><code>numTrade&lt;-0
  
  numUnits&lt;-c(0,1,0,-1,1,1,-1)
  
  for(i in 2:length(numUnits)){
    if(numUnits[i]&gt;0 &amp; numUnits[i-1]&lt;0){
      numTrade&lt;-numTrade+1
    }
    else if(numUnits[i]&lt;0 &amp; numUnits[i-1]&gt;0){
      numtrade&lt;-numTrade+1
    }
      else if(numUnits[i]!=0 &amp; numUnits[i-1]==0){
        numTrade&lt;-numTrade+1
      } 
        else {
          numTrade&lt;-numTrade+0
        }
    print(numTrade)
  } 
  </code></pre>