title: sapply with custom function (series of if statements)
body: <p>I want to run a function which looks at two vectors, returning different values depending on the signs of the values in the two vectors. I have written a function which works to compare two values, but then I want to run this on two vectors. So I used sapply, but I am getting different results than expected.</p>
  
  <pre><code>bear.correction&lt;-  function(x,y){
                                  if(x &gt; 0 &amp;&amp; y &lt; 0){
                                    return(90)
                                  }else if(x &lt; 0 &amp;&amp; y &lt; 0){
                                    return(180)
                                  }else  if(x &lt; 0 &amp;&amp; y &gt; 0){
                                    return(270)
                                  }else   return(0)
                                }
  </code></pre>
  
  <p>The following give the expected (and desired) result:</p>
  
  <pre><code>  bear.correction(1,-1)
    bear.correction(1,1)
    bear.correction(-1,1)
    bear.correction(-1,-1)
  </code></pre>
  
  <p>Result: 90, 0, 270, 180</p>
  
  <p>However when I try to do the same comparisons, but using vectors with sapply I get a different result:</p>
  
  <pre><code>  x &lt;- c(1,1,-1,-1)
    y &lt;- c(-1,1,1,-1)
    sapply(x,bear.correction,y)
  </code></pre>
  
  <p>Result: 90, 90, 180, 180.</p>
  
  <p>I can't see what's wrong, so please help!</p>