This post was last Updated on by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.
In this tutorial, we will discuss R if elseif or conditional statements. Conditional statements are used to check the conditions in programming. R language supports the following conditional statements.
- if
- if-else
- nested if
Let’s see them one by one with examples.
Before going to this, we have to understand what operators are.
Also Read: How to Write a Loop in R
R Conditional Operators
Operators are used to performing logical conditions used inside conditional statements.
They are:
- == Equal operator (Compare two variables equal or not)
- > Greater than operator (Compare the first variable is greater than the second variable)
- < Less than operator (Compare the first variable is less than the second variable)
- >= Greater than or equal to an operator (Compare the first variable is greater than or equal to the second variable)
- <= Less than or equal to an operator (Compare the first variable is less than or equal to the second variable)
- != Not Equal operator (Compare two variables not equal)
Also Read: How to Merge Data in R
R If Statement
If statement is used to execute a block if the condition specified in it returns TRUE. We can check the conditions inside the if statement using the conditional operators mentioned above.
Syntax:
if(condition) { statements ---------- }
If the condition satisfies, it will go inside the if block and executes the statements; otherwise, it will not execute the if block.
Example 1:
In this example, we will create two variables and check whether the conditions satisfy or not.
#create variables my_var1=45 my_var2=41 #check first variable is equal to second variable if(my_var1==my_var2) { print("Both are equal") } #check first variable is not equal to second variable if(my_var1!=my_var2) { print("Both are not equal") } #check first variable is greater than second variable if(my_var1>my_var2) { print("First variable is greater than second variable") } #check first variable is less than second variable if(my_var1<my_var2) { print("First variable is less than second variable") } #check first variable is greater than or equal to the second variable if(my_var1>=my_var2) { print("First variable is greater than or equal to the second variable") } #check first variable is less than or equal to the second variable if(my_var1<=my_var2) { print("First variable is less than or equal to the second variable") }
Output:
[1] "Both are not equal" [1] "First variable is greater than second variable" [1] "First variable is greater than or equal to the second variable"
We can see from the above output that only three conditions are executed out of six conditions.
Also Read: Matrices in R | How to Create, Update, and Modify R Matrices
Example 2:
In this example, we will create two variables and check whether the multiple conditions satisfy or not.
Here, we check multiple conditions using or (||), and (&&) operators.
Or operator returns TRUE when any of the conditions return TRUE.
And operator returns TRUE when both the conditions return TRUE.
#create variables my_var1=45 my_var2=41 #check the first variable is greater than second variable or second variable is greater than first variable if(my_var1>my_var2 || my_var1<my_var2) { print("first variable is greater than second variable or second variable is greater than first variable") } #check the first variable is greater than second variable and second variable is greater than first variable if(my_var1>my_var2 && my_var1<my_var2) { print("first variable is greater than second variable or second variable is greater than first variable") }
Output:
[1] "first variable is greater than second variable or second variable is greater than first variable"
From the output, we have seen that the first statement is executed.
Also Read: 10 Best Mouse for Programming [2022]
R If Elseif Conditional Statement
if-else statement is used to execute a block of codes if the condition specified in it returns TRUE. We can check the conditions inside the if statement and else use the above conditional operators.
If the condition in the block fails, it will execute the other block.
Syntax:
if(condition) { statements ---------- }else{ statements ---------- }
If the condition satisfies inside if, it executes the statements; otherwise, it will go inside the else block.
Also Read: How To Learn Data Science [Beginner’s Guide]
Example :
In this example, we will create two variables and check whether the conditions satisfy or not.
#create variables my_var1=45 my_var2=41 #check first variable is equal to second variable or not if(my_var1==my_var2) { print("Both are equal") }else{ print("Both are not equal") } #check first variable is not equal to second variable if(my_var1!=my_var2) { print("Both are not equal") }else { print("Both are equal") } #check first variable is greater than second variable or not if(my_var1>my_var2) { print("First variable is greater than second variable") }else { print("First variable is not greater than second variable") } #check first variable is less than second variable or not if(my_var1<my_var2) { print("First variable is less than second variable") }else { print("First variable is not less than second variable") } #check first variable is greater than or equal to the second variable or not. if(my_var1>=my_var2) { print("First variable is greater than or equal to the second variable") }else { print("First variable is neither greater than nor equal to the second variable") } #check first variable is less than or equal to the second variable or not if(my_var1<=my_var2) { print("First variable is less than or equal to the second variable") }else { print("First variable is neither less than nor equal to the second variable") }
Output:
[1] "Both are not equal" [1] "Both are not equal" [1] "First variable is greater than second variable" [1] "First variable is not less than second variable" [1] "First variable is greater than or equal to the second variable" [1] "First variable is neither less than nor equal to the second variable"
Also Read: 15 Best Python Libraries for Data Science and Analysis
R Nested If Statements
Nested if statements are used to execute a block of codes if the condition specified in it returns TRUE. If the condition returns TRUE, it will check again inside in an If-else condition.
We can check the conditions inside the if statement and else use the above conditional operators.
If the condition in the block fails, it will execute the other block.
Syntax:
if(condition) { if(condition) { statements ---------- }else{ statements ---------- } }else{ statements ---------- }
If the condition satisfies inside if, then it executes the statements. Otherwise, it will go inside the else block.
Example:
In this example, we will create two variables and check whether the first variable is equal to the second variable. If equal, check first is greater than or equal to the second.
#create variables my_var1=45 my_var2=45 #check first variable is equal to second variable or not. if equal check first is greater than or equal to second if(my_var1==my_var2) { if(my_var1>=my_var2) { print("first variable is greater than or equal to second variable") }else{ print("first variable is neither greater nor equal to the second variable") } }else{ print("Both are not equal") }
Output:
[1] "first variable is greater than or equal to second variable"
Also Read: 10 Best Data Science Coursera Courses For Beginners
Summary
From this tutorial, we saw how to use if conditional operators with if and else statements, and we have seen how to execute multiple conditions within a single conditional statement.