R Conditionals
In R, conditionals help control the flow of a program. They evaluate Boolean expressions and execute certain blocks of code depending on whether the expression evaluates to TRUE or FALSE. This is especially useful while working with dynamic data, automating tasks, or writing functions with variable behavior.
R If Statement
The R if statement is used to run a block of code if a condition is TRUE.
Here is a flowchart that showcases the flow of the if statement in R:

R If Statement Syntax
if (condition) {
# Code to execute if condition is TRUE
}
R If Statement Example
This example demonstrates the usage of the if statement in R:
x <- 10if (x > 6) {print("x is greater than 6")}
Here is the output:
[1] "x is greater than 6"
R If-Else Statement
The R if-else statement allows the execution of a block of code if a condition is TRUE, and a separate block of code if the condition is FALSE.
R If-Else Statement Syntax
if (condition) {
# Code to execute if condition is TRUE
} else {
# Code to execute if condition is FALSE
}
R If-Else Statement Example
This example demonstrates the usage of the if-else statement in R:
x <- 3if (x > 6) {print("x is greater than 6")} else {print("x is less than or equal to 6")}
Here is the output:
[1] "x is less than or equal to 6"
R Else-If Statement
When there is a need to check multiple conditions, the R else-if statement can be used to evaluate them sequentially.
R Else-If Statement Syntax
if (condition1) {
# Code if condition1 is TRUE
} else if (condition2) {
# Code if condition2 is TRUE
} else {
# Code if all conditions are FALSE
}
R Else-If Statement Example
This example demonstrates the usage of the else-if statement in R:
x <- 5if (x > 12) {print("x is greater than 12")} else if (x == 5) {print("x is equal to 5")} else {print("x is less than 12 and not equal to 5")}
Here is the output:
[1] "x is equal to 5"
Nested R If Statement
R if statements can be nested within each other for more complex logical checks.
Nested R If Statement Syntax
if (condition1) {
if (condition2) {
# Code to execute if both condition1 and condition2 are TRUE
}
}
Nested R If Statement Example
This example demonstrates the usage of nested if statements in R:
x <- 8y <- 3if (x > 6) {if (y < 6) {print("x is greater than 6 and y is less than 6")}}
Here is the output:
[1] "x is greater than 6 and y is less than 6"
Frequently Asked Questions
1. Can I use conditionals in a vectorized way in R?
Yes, you can use the R ifelse() function for vectorized operations:
x <- c(2, 5, 8)result <- ifelse(x > 5, "High", "Low")print(result) # Output: [1] "Low" "Low" "High"
2. What’s the difference between R if and ifelse()?
ifstatement in R is used for single, scalar conditions.ifelse()function in R is vectorized and works over entire vectors.
3. Can I use logical operators in R conditionals?
Yes. You can combine conditions using & (AND), | (OR), and ! (NOT):
x <- 4if (x > 2 & x < 10) {print("x is between 2 and 10")} # Output: [1] "x is between 2 and 10"
4. What are the three elements of the if statement in R?
The three elements of an if statement in R are:
- Condition: A logical expression that evaluates to
TRUEorFALSE. - Code block for
TRUE: The set of statements to execute if the condition isTRUE. - Optional
elseblock: A set of statements to run if the condition isFALSE.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn R on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to code and clean and manipulate data for analysis and visualization with the R programming language.
- Beginner Friendly.14 hours