title: If else within function
body: <p>Reading <a href="http://adv-r.had.co.nz/Functional-programming.html" rel="nofollow noreferrer">http://adv-r.had.co.nz/Functional-programming.html</a> I'm attempting to modify a function to update a vector of values where 10 is replaced with 110 and all others are replace with 2.</p>
  
  <p>Here is my code to replace 10 with 110 : </p>
  
  <pre><code>replacer &lt;- function(x) {
    x[x == 10] &lt;- 110
    x
  }
  
  replacer(c(0, 10))
  </code></pre>
  
  <p>This works as expected and returns <code>0 110</code> but how to modify in order to set values to 2 where 10 is not encountered ?</p>
  
  <p>I've tried : </p>
  
  <pre><code>replacer &lt;- function(x) {
   if (x[x == 10] &lt;- 110)
    x
   else
    2
  }
  
  replacer(c(0, 10))
  </code></pre>
  
  <p>Which returns same result : <code>0 110</code> but I'm expecting <code>2 110</code></p>
  
  <p>Update : </p>
  
  <p>Using  Sotos comment solution is : </p>
  
  <pre><code>replacer &lt;- function(x) {
      ifelse( x == 10, 110, 2) 
  }
  
  replacer(c(0, 10))
  </code></pre>