How to Use If-Else Statements in R

When using R, sometimes you need your function to do something if a condition is true and something else if it is not. You could do this with two if statements, but there’s an easier way in R: an if…else statement. An if…else statement contains the same elements as an if statement (see the preceding section), and then some extra:

  • The keyword else, placed after the first code block
  • A second block of code, contained within braces, that has to be carried out if and only if the result of the condition in the if() statement is FALSE

In some countries, the amount of value added tax (VAT) that has to be paid depends on whether the client is a public or private organization. Imagine that public organizations have to pay only 6 percent VAT and private organizations have to pay 12 percent VAT. You can add an extra argument public to the priceCalculator() function and adopt it as follows to add the correct amount of VAT:

if(hours > 100) net.price <- net.price * 0.9
if(public) {
  tot.price <- net.price * 1.06
} else {
  tot.price <- net.price * 1.12

If you send this code to the console, you can test the function. For example, if you worked for 25 hours, the following code gives you the different amounts you charge for public and private organizations, respectively:

[1] 1060
[1] 1120

This works well, but how does it work?

If you look at the if…else statement in the previous function, you find these elements. If the value of the argument public is TRUE, the total price is calculated as 1.06 times the net price. Otherwise, the total price is 1.12 times the net price.

The if statement needs a logical value between the parentheses. Any expression you put between the parentheses is evaluated before it’s passed on to the if statement. So, if you work with a logical value directly, you don’t have to specify an expression at all. Using, for example, if(public == TRUE) is redundant.

Also, in the case of an if…else statement you can drop the braces if both code blocks exist of only a single line of code. So, you could just forget about the braces and squeeze the whole if…else statement on a single line. Or you could even write it like this:

if(public) tot.price <- net.price * 1.06 else
           tot.price <- net.price * 1.12

Putting the else statement at the end of a line and not the beginning of the next one is a good idea.

In general, R reads multiple lines as a single line as long as it’s absolutely clear that the command isn’t finished yet. If you put else at the beginning of the second line, R considers the first line finished and complains. You can put else at the beginning of a next line only if you do so within a function and you source the complete file at once to R.

But you can make this shorter. The if statement works like a function and, hence, it also returns a value. As a result, you can assign that value to an object or use it in calculations. So, instead of recalculating net.price and assigning the result to tot.price within the code blocks, you can use the if…else statement like this:

tot.price <- net.price * if(public) 1.06 else 1.12

R will first evaluate the if…else statement, and multiply the outcome by net.price. The result of this is then assigned to tot.price. This differs not one iota from the result of the five lines of code we used for the original if…else statement.