title: R - How to Create Custom Ifelse function that repeats
body: <p>I am quite familiar with R's standard ifelse statement, and how to create nested ifelse statements. I however want to create a "better" version, so that I dont have to copy / paste ifelse so many times. </p>
  
  <p>Take this nested ifelse statement for example:</p>
  
  <pre><code>df &lt;- data.frame(b = 1:5)
  
  df$a &lt;- ifelse(df$b == 1,1,
          ifelse(df$b == 2,2,
          ifelse(df$b == 3,3,4)))
  </code></pre>
  
  <p>Instead, what I would like to do is create a function like I could call like this:</p>
  
  <pre><code>df$a &lt;- myFunction(df$b == 1,1,
                     df$b == 2,2,
                     df$b == 3,3,4)
  </code></pre>
  
  <p>I would want the function to be able to pick up how many arguments I have entered, and thus know how many ifelse statements to include and then plug the arguments into the correct position, up to however many I want. </p>
  
  <p>There is still some repetition, but when creating longer nested ifelse statements it would be nice to not have to repeat that piece of code, and then try to keep track of ending paren's. </p>