if_else R Function of dplyr Package (2 Examples)

 

In this tutorial you’ll learn how to apply the if_else function of the dplyr package in R programming.

Table of contents:

Let’s start right away!

 

Example 1: Basic Application of if_else Function

In this example you’ll learn the basic R syntax of the if_else function. First, we need to install and load the dplyr package to R:

install.packages("dplyr")                        # Install dplyr
library("dplyr")                                 # Load dplyr

Then, we also have to create an example vector, to which we can apply the if_else function:

x <- -3:3                                        # Example vector
x
# -3 -2 -1  0  1  2  3

Our example vector contains seven numeric values ranging from -3 to +3.

Now, we can apply the if_else function as follows:

if_else(x < 0, "neg", "pos")                     # Basic application of if_else
# "neg" "neg" "neg" "pos" "pos" "pos" "pos"

The previous syntax consists of three parts:

  1. x < 0 is the logical condition
  2. “neg” is the output that we return in case our logical condition is TRUE
  3. “pos” is the output that we return in case our logical condition is FALSE

As you can see based on the previously shown output of the RStudio console, the first three elements of our vector are negative and the last four elements of our vector are positive.

You may wonder what the difference of dplyr’s if_else statement is in comparison to the ifelse function, which is already provided by the basic installation of the R programming language. Let’s have a look that the documentation of dplyr says on this topic:

Compared to the base ifelse(), this function is more strict. It checks that true and false are the same type. This strictness makes the output type more predictable, and makes it somewhat faster.

 

Example 2: Apply if_else Function to Vector with NAs

We can also specify an output for the if_else command in case there are NA values in our input vector. Let’s create an example vector with NA values:

x_NA <- c(-3:3, NA)                              # Example vector with NA
x_NA
# -3 -2 -1  0  1  2  3 NA

Now, we can specify an additional output within the if_else function for the case when a vector element is missing:

if_else(x_NA < 0, "neg", "pos", "xxx")           # Apply if_else with NA condition
# "neg" "neg" "neg" "pos" "pos" "pos" "pos" "xxx"

As you can see, the output is exactly the same as in Example 1, but this time with an additional output for the NA value that we added at the end of our vector.

 

Video, Further Resources & Summary

Would you like to learn more about the if_else function? Then you could have a look at the following video of my YouTube channel. In the video, I illustrate the examples of this tutorial in RStudio:

 

The YouTube video will be added soon.

 

In addition to the video, you could have a look at the related articles on this homepage. I have published numerous posts about dplyr package and its functions already:

 

At this point you should have learned how to use the if_else statement of the dplyr package in the R programming language. In case you have further questions, let me know in the comments.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top