title: Using Apply to avoid "the condition has length > 1 and only the first element will be used"?
body: <p>I'm trying to define a piece-wise function, and it works fine for single inputs, but when I try to use a vector input, I get the "the condition has length > 1 and only the first element will be used" error because it's passing a scalar into a vector as far as I can tell. </p>
  
  <pre><code>ex &lt;- function(x){
    if (x &lt; -1) {
      y &lt;- (-2 * x) -2
      return(y)
    }
    else if (-1 &lt;= x &amp; x &lt;= 1) {
      y &lt;- 0
      return(y)
    }
    else if (1 &lt; x){
      y &lt;- (x * x) - 1
      return(y)
    }
  }
  </code></pre>
  
  <p>Can anyone help me use an apply function to fix this issue? I'd like to avoid using ifelse.</p>