Nested if-else statement in R
Last Updated :
29 Nov, 2022
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 type using 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 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
Similar Reads
IF-ELSE-IF statement in R 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 arit
2 min read
Nested If Else Statement in Programming Nested If Else Statements are a fundamental concept in programming. They allow us to create more complex decision-making structures by placing one if else statement inside another. In this article, we will discuss the Nested if else statement. Table of Content What is Nested If Else Statement?Syntax
6 min read
Nested-if statement in Python For more complex decision trees, Python allows for nested if statements where one if statement is placed inside another. This article will explore the concept of nested if statements in Python, providing clarity on how to use them effectively.Python Nested if StatementA nested if statement in Python
2 min read
Nested-if statement in Python For more complex decision trees, Python allows for nested if statements where one if statement is placed inside another. This article will explore the concept of nested if statements in Python, providing clarity on how to use them effectively.Python Nested if StatementA nested if statement in Python
2 min read
Address Errors in 'if' Statements in R Error handling is super important when writing code, no matter which language are using even in R Programming Language. Errors can happen for all sorts of reasons, like when a user gives the computer the wrong kind of information, something happens that the user didn't expect, or the user just makes
5 min read
VBA Multiple (Nested) If Statements in Excel VBA in Excel stands for Visual Basic for Applications, which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. What decision-making reasoning do you often use in your Excel worksheets? In most circums
5 min read