Nested if-else statement in R - GeeksforGeeks

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 valuesvar1 <- 6var2 <- 5var3 <- -4  # checking if-else if ladderif(var1 > 10 || var2 < 5){  print("condition1")}else{  if(var1 <4 ){    print("condition2")  }else{    if(var2>10){      print("condition3")    }    else{      print("condition4")    }  }}
Output:
[1] "condition4"
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
Picked
R-basics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
Time Series Analysis in R
R - if statement
How to filter R dataframe by multiple conditions?