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.


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


Furthermore, you could read the other tutorials on 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.

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.