Skip to content
Related Articles

Related Articles

Nested if-else statement in R

Improve Article
Save Article
Like Article
  • Last Updated : 23 Sep, 2021

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 statement better justifies the definition.

Syntax:

if(condition1){
# execute only if condition 1 satisfies
if(condition 2){ 
# execute if both condition 1 and 2 satisfy
}
}else{
}

Example: Nested if-else statement

R




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

Output:

[1] "condition4"

Using ifelse statement

The first argument in the ifelse() method contains the condition to be evaluated. The second and third arguments contain the value on true and false evaluation of the condition respectively. In the case of evaluation with dataframe or other R objects, the columns are referred to typiusing the dataframe name. 

Syntax:

ifelse(cond, value-on-true, value-on-false)

Example: Nested if-else using ifelse

R




# creating a dataframe 
data_frame <- data.frame(col1 = c(1:9),
                         col2 = LETTERS[1:3])
  
print("Original DataFrame")
print(data_frame)
  
data_frame$col3 = ifelse(data_frame$col1>4,"cond1 satisfied",
                         ifelse(data_frame$col2 %in% c("A","C"),
                                "cond2 satisfied",
                         "both failed"))
  
print("Modified DataFrame")
print(data_frame)

Output:

[1] "Original DataFrame" 
col1 col2 
1    1    A 
2    2    B 
3    3    C 
4    4    A 
5    5    B 
6    6    C 
7    7    A 
8    8    B 
9    9    C 
[1] "Modified DataFrame"
col1 col2            col3 
1    1    A cond2 satisfied 
2    2    B     both failed 
3    3    C cond2 satisfied 
4    4    A cond2 satisfied 
5    5    B cond1 satisfied 
6    6    C cond1 satisfied 
7    7    A cond1 satisfied 
8    8    B cond1 satisfied 
9    9    C cond1 satisfied

In the case of the nested dataframe, the nested conditions contain the dataframe name again and again. To save from this complexity and increase efficiency, the dataframe name is specified in the first argument of the with() method.

Syntax: 

with(data-frame , ifelse())

Example: Using with() with ifelse()

R




# creating a dataframe 
data_frame <- data.frame(col1 = c(1:9),
                         col2 = LETTERS[1:3])
  
print("Original DataFrame")
print(data_frame)
  
data_frame$col3 = with(data_frame, 
                       ifelse(col1>4,"cond1 satisfied",
                        ifelse(col2 %in% c("A","C"),
                               "cond2 satisifed",
                           "both failed")))
    
print("Modified DataFrame")
print(data_frame)

Output

[1] "Original DataFrame" 
col1 col2 
1    1    A 
2    2    B 
3    3    C 
4    4    A 
5    5    B 
6    6    C 
7    7    A 
8    8    B 
9    9    C 
[1] "Modified DataFrame"
col1 col2            col3 
1    1    A cond2 satisfied 
2    2    B     both failed 
3    3    C cond2 satisfied 
4    4    A cond2 satisfied 
5    5    B cond1 satisfied 
6    6    C cond1 satisfied 
7    7    A cond1 satisfied 
8    8    B cond1 satisfied 
9    9    C cond1 satisfied

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!