If Else Statement in R (3 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:

  1. Check whether a logical condition (i.e. x1 == 1) is TRUE.
  2. 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:

  1. The logical condition we want to test.
  2. What should happen in case the logical condition is TRUE.
  3. 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.

 

Video, Further Resources & Summary

If you need further info on the R syntax of this tutorial, I can recommend to have 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.

 

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.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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