Nested ifelse Statement in R (2 Examples)

 

In this article you’ll learn how to apply nested ifelse statements in the R programming language.

Table of contents:

Here’s how to do it.

 

Example 1: Nested ifelse Statement with Multiple TRUE Conditions

This section illustrates how to nest two ifelse statements in R. Have a look at the following R code:

ifelse(test = 5 > 3,                  # First test condition
 
       yes = ifelse(test = 5 > 4,     # Second test condition
                    yes = "TRUE Twice",
                    no = "Yes & No"),
 
       no = "No")
# "TRUE Twice"

The previous R syntax nests two ifelse statements. The second ifelse statement is applied in case the first logical test condition is TRUE.

In this example, the first and the second test conditions are TRUE. For that reason, the nested ifelse statement returns the output “TRUE Twice”.

 

Example 2: Nested ifelse Statement with TRUE & FALSE Conditions

Example 2 shows how to create a nested ifelse statement where the first condition is TRUE and the second condition is FALSE:

ifelse(test = 5 > 3,                  # First test condition
 
       yes = ifelse(test = 5 <= 4,    # Second test condition
                    yes = "TRUE Twice",
                    no = "Yes & No"),
 
       no = "No")
# "Yes & No"

The output is “Yes & No”.

 

Video & Further Resources

Do you need more information on the content of this tutorial? Then you may watch the following video of the Statistics Globe YouTube channel. I’m explaining the contents of this article in the video.

 

 

Furthermore, you could read the other tutorials on https://statisticsglobe.com/. You can find some articles below.

 

At this point you should have learned how to nest two ifelse functions in the R programming language. In case you have additional questions, please let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

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


2 Comments. Leave new

  • Better use dplyr::case_when() (https://dplyr.tidyverse.org/reference/case_when.html) than writing nested ifelse statments. It’s easier to read and debug.

    Reply
    • Hello Frederik,

      Each user has their own preference in coding. Therefore we provide as much as options as we can. We also have a tutorial about case_when. You can take a look. By the way, you are right about the advantages of case_when(). However, we can argue that the user can still prefer ifelse() since it has a simple structure based on basic conditional logic and it does not require any additional package to be installed, contrary to case_when().

      Thank you for your input. I am sure that our readers benefit from it.

      Regards,
      Cansu

      Reply

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.

Top