If Else Statement in R (4 Examples)
In this R tutorial you’ll learn how to use different types of if and else statements.
The article looks as follows:
Let’s dive right into the exemplifying R code!
Example 1: Applying if() and else() Functions in R
Example 1 illustrates how to use the if and else functions in combination. First, we have to define a data object that we will use in the if and else statements:
x1 <- 5 # Example data object
Now, we can run the if and else functions as shown below:
if(x1 == 1) { # Using if function cat("If condition was TRUE") } else { # Using else function cat("If condition was FALSE") } # If condition was FALSE
Explanation of the previous R code:
- Check whether a logical condition (i.e. x1 == 1) is TRUE.
- If the logical condition is not TRUE, apply the content within the else statement (i.e. return the sentence “If condition was FALSE”).
Note that in case the logical condition within the if statement is TRUE, the else statement is automatically skipped.
Example 2: Applying ifelse() Function in R
This Example shows how to use the ifelse function instead of the if and else statements shown in Example 1. Within the ifelse function, we have to specify three parameters:
- The logical condition we want to test.
- What should happen in case the logical condition is TRUE.
- What should happen in case the logical condition is FALSE.
Have a look at the following R syntax:
ifelse(test = x1 == 1, # Using ifelse function yes = "If condition was TRUE", no = "If condition was FALSE") # If condition was FALSE
Since our logical condition is FALSE, the ifelse function is returning the sentence “If condition was FALSE”.
Example 3: Applying ifelse() Function in for-Loop
In Example 3, I’ll illustrate how to apply an if else statement within a for-loop. First, we are defining a vector object that we can use within the for-loop:
x2 <- 1:10 # Example vector
Now, we are running our loop using the ifelse function within the loop:
for(i in 1:length(x2)) { # Using ifelse function in loop cat(ifelse(test = x2[i] == 1, yes = "If condition was TRUE", no = "If condition was FALSE"), "\n") } # If condition was TRUE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE
Note that we had to wrap the cat function around the ifelse function, because character strings are otherwise not printed within our for-loop.
Example 4: Applying Vectorized ifelse() Statement
One advantage of the ifelse function is that we can use it as vectorized if statement.
The R code of Example 3 could be simplified by assigning an entire vector to the test argument of the ifelse function.
Have a look at the following R code:
ifelse(test = x2 == 1, # Vectorized ifelse statement yes = "If condition was TRUE", no = "If condition was FALSE") # If condition was TRUE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE # If condition was FALSE
As you can see, we have created exactly the same output as in the previous example, but the R syntax was much less complicated.
Video, Further Resources & Summary
If you need further info on the R syntax of this tutorial, I can recommend having a look at the following video of my YouTube channel. In the video, I’m explaining the R programming code of this page in a live session in R.
The YouTube video will be added soon.
Furthermore, you may have a look at the related articles of this homepage. Some articles can be found here.
- Nested ifelse Statement in R
- dplyr mutate Function with Logical ifelse Condition
- if_else R Function of dplyr Package
- Loops in R
- R Functions List (+ Examples)
- The R Programming Language
Summary: At this point you should have learned how to use the if, else, and ifelse functions in the R programming language. If you have further questions, please let me know in the comments.