Open In App

IF-ELSE-IF statement in R

Last Updated : 05 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

if-else-if ladder in R Programming Language is used to perform decision making. This ladder is used to raise multiple conditions to evaluate the expressions and take an output based on it. This can be used to evaluate expressions based on single or multiple conditions connected by comparison or arithmetic operators. It is particularly useful to check a list of conditions within a single loop. 

Syntax:

if(outer-condition is true) {
       execute this statement
} else if(inner-condition1 is true) {
       execute this statement
} .
  .
  .
  .
else {
       execute this statement
}

There can be more than one else if statement in the ladder to check for a lot of conditions at the same time, in this case, it works like a switch. The following code snippets indicate the illustration of the if-else-if ladder.

Example: if-else if-else ladder

R




# creating values
a <- 'A'
  
# checking if-else if ladder
if(a %in% c('E','D')){
    print("Block if")
  }else if(a %in% c('A','D'))
  {
    print("Block else-if")
  }else
  {
    print("Block else")
  }


Output

[1] "Block else-if" 

Example: if-else if-else ladder

R




# creating values
var1 <- 6
var2 <- 5
  
# checking if-else if ladder
if(var1 > 10 || var2 < 5){
  print("condition1")
}else if(var1<7 && var2==5){
  print("condition2")
}


Output

[1] "condition2"

Example: if-else if-else ladder

R




# creating values
var1 <- 6
var2 <- 5
var3 <- -4
  
# checking if-else if ladder
if(var1 > 10 || var2 < 5){
  print("condition1")
}else if(var1<7 && var2==5 && var3>0){
  print("condition2")
}else if(var1<7 && var2==5 && var3<0){
  print("condition3")
}else{
  print("condition4")
}


Output

[1] "condition3"


Similar Reads

Decision Making in R Programming - if, if-else, if-else-if ladder, nested if-else, and switch
Decision making is about deciding the order of execution of statements based on certain conditions. In decision making programmer needs to provide some condition which is evaluated by the program, along with it there also provided some statements which are executed if the condition is true and optionally other statements if the condition is evaluat
5 min read
Nested if-else statement in R
In this article, we will discuss the nested if-else statement in the R programming language. The if-else statements can be nested together to form a group of statements and evaluate expressions based on the conditions one by one, beginning from the outer condition to the inner one by one respectively. An if-else statement within another if-else sta
3 min read
R If Else Conditions
The if-statement in Programming Language alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false? Here comes the R Programming Language else statement. We can use the else statement with the if statement to execute a b
5 min read
R Next Statement
Next statement in R is used to skip any remaining statements in the loop and continue the execution of the program. In other words, it is a statement that skips the current iteration without loop termination. 'next' is a loop control statement just like the break statement. But 'next' statement works opposite to that of the break statement, instead
6 min read
R - if statement
If statement is one of the Decision-making statements in the R programming language. It is one of the easiest decision-making statements. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not. Syntax: if (expression) { #s
3 min read
goto statement in R Programming
Goto statement in a general programming sense is a command that takes the code to the specified line or block of code provided to it. This is helpful when the need is to jump from one programming section to the other without the use of functions and without creating an abnormal shift. Unfortunately, R doesn't support goto but its algorithm can be e
2 min read
Case when statement in R Dplyr Package using case_when() Function
This article focuses upon the case when statement in the R programming language using the case_when() function from the Dplyr package. Case when is a mechanism using which we can vectorize a bunch of if and else if statements. In simple words, using a case when statement we evaluate a condition expression, and based on that we make decisions. For e
4 min read
Article Tags :
three90RightbarBannerImg