R is a data science and data analysis language that deals with vectors all the time. To make decision-making quicker with vectorized data, there’s a built-in ifelse function you can call on vectors. This function performs an elementwise if…else check on the vector and returns a result vector based on the conditions.
In a sense, the ifelse function is a replacement for combining a for loop with an if…else statement for performing checks on vectors.
This is a complete guide to the ifelse function in R. The theory is backed up with illustrative examples.
What Is the ifelse Function in R?
Data science involves dealing with matrices and vectors of data. One of the fundamental building blocks of the R programming language is the vectors. A typical R program takes a vector input and returns a vector output.
Another key concept in R (or any other language) is the ability to add logic and decision-making to your code. Any sophisticated piece of code should include logic such as when to turn left and when to turn right.
In R, you can use if…else statements to make decisions in your code.
But because a traditional if…else statement is meant for checking singular values rather than vector data, there’s also a separate built-in ifelse function for vectors in R. This function is a vector version of the if…else statement. It’s not only a convenient replacement for if…else statements but also performs better.
Before seeing examples, let’s take a look at the syntax of the ifelse function in R.
Syntax
ifelse(condition_expr, true_elem, false_elem)
Where:
- condition_expr is a vector result of a logical operation applied to each element in an input vector.
- true_elem is included in the result vector if an element in the condition_expr has a value TRUE.
- false_elem is included in the result vector if an element in the condition_expr has a value FALSE.
The ifelse function returns a vector that is as long as the input vector.
The best way to understand how the ifelse function works in R is by having a look at some examples.
Example
The ifelse function applies an if…else-style operation for each element of the input vector. Based on the result each element gives, the ifelse function places a true_elem or false_elem into a result vector.
As an example, let’s check which numbers are odd and which are even in a vector of numbers so that the result is a vector of strings “Even“/”Odd“:
a = c(1, 2, 3, 4, 5, 6, 7) ifelse(a %% 2 == 0, "Even", "Odd")
Output:
[1] "Odd" "Even" "Odd" "Even" "Odd" "Even" "Odd"
To make it easier to understand the above example, let’s break the code down into pieces.
A Step-by-Step Explanation
First of all, to be an even number, a number must be evenly divisible by 2. In other words, the result of the number modulo 2 must return 0.
To calculate the remainder in the division in R, use the modulo operator %%.
When you call the %% operator on a vector and a number, the result is a vector where the %% operation has been applied between each element and the divisor.
Now, let’s focus on the ifelse call in the example, ifelse(a %% 2 == 0, “Even”, “Odd”).
Here a %% 2 == 0 produces a vector [0, 1, 0, 1, 0, 1, 0] and takes each value from the list [0, 1, 0, 1, 0, 1, 0] and compares them with 0. So the result of a %% 2 == 0 is [FALSE , TRUE, FALSE , TRUE, FALSE, TRUE, FALSE].
With this boolean vector, the ifelse function places “Even” into the result vector when it encounters TRUE and “Odd” when it encounters FALSE. This gives the result of “Odd” “Even” “Odd” “Even” “Odd” “Even” “Odd”
I hope this clarifies how the ifelse function works. Before you go, let’s take a look at another example.
The Same Example Using if…else Statements
Let’s repeat the above example but instead of using the ifelse function, let’s use an if…else statement and a for loop:
result <- c() for (x in a){ if (x %% 2 == 0){ result <- c(result, "Even") } else { result <- c(result, "Odd") } } result
Output:
[1] "Odd" "Even" "Odd" "Even" "Odd" "Even" "Odd"
The result is the same but as you can tell, there’s much more code to write. And as it turns out, doing it this way is typically much slower too!
if…else Statements vs ifelse Function in R
Now that you’ve learned how to use the ifelse function in R, you may wonder which is best: the ifelse function or if…else statements.
When it comes to convenience, using ifelse function on vectors is much easier and more streamlined than using the if…else statements. You could already see this in the previous examples as there was much less code to write with the ifelse function.
But another aspect is performance. The ifelse function is typically much faster than performing an if…else check on the elements of a vector.
For example, let’s create a vector of 100 000 random normally distributed values and check whether each individual value is positive or negative. Let’s use both ifelse function and the if…else statements with a for loop to compare the performance:
In case you’re familiar with computing runtimes, make sure to read my complete guide to measuring the execution time in R.
library(rbenchmark) x <- rnorm(100000) benchmark(replications = 100, { y <- ifelse(x < 0, "-", "+") })$user.self benchmark(replications = 100, { y <- c() for (i in x) { if (i < 0) { y[length(y)+1] <- "-" } else { y[length(y)+1] <- "+" } } })$user.self
Output:
[1] 4.59 [1] 10.89
From this result, you can see that the ifelse approach is more than twice as fast as the if…else approach. So not only is the ifelse function convenient but also fast!
Summary
Today you learned how to use the built-in ifelse function in R.
To take home, ifelse is a quick and convenient replacement for the if…else statements for vectors.
The ifelse function takes a vector of booleans (or boolean convertible values) and creates a result vector based on if the elements are TRUE or FALSE. The idea of the ifelse function is the same as combining a for loop and an if…else statement to do elementwise checks for vectors.
Thanks for reading. Happy coding!