If statement can be followed by the optional else statement, which executes when the boolean expression is FALSE. If the boolean expression evaluates TRUE, then the if block will be executed; otherwise, the else block will be executed. Thus, you can make a decision in R programming using the conditional If…Else statement.
If-else statements are control structures in any programming language. The control structures are the blocks of code that determine how other code sections are executed based on defined parameters.
Flow chart of if-else in R
R if statement
See the following syntax of if statement in R.
if (condition) {
expression
}
If the condition is TRUE, the statement gets executed. But if it’s FALSE, then nothing will happen.
If the condition can be a logical or numeric vector, but only the first item is considered.
In the case of a numeric vector, zero is taken as FALSE; the rest is TRUE.
qunatity <- 12
if (qunatity > 0) {
print("You need to purchase dozen")
}
Output
[1] "You need to purchase dozen"
R if-else statement
An if-else statement is a great way for the developer to return output based on the condition.
In R, the syntax of if-else is the following.
if (condition) {
expression A
} else {
expression B
}
See the below example.
quantity <- 12
if (quantity == 12) {
print("It is a dozen")
} else {
print("It is not a dozen")
}
Output
[1] "It is a dozen"
If the quantity is not 12, then else block will be executed. In our case, it is 12, so if block is executed.
R If-Else Statement
The “if statement” can be followed by the optional else if…else statement, which is beneficial to test various conditions using the single if…else if statement.
When you are using if-else if, else statements, there are few points to keep in mind.
- If statement can have zero or one else, and it must come after any else if’s.
- If statement can have zero to many else if’s and they must come before the else.
- Once an else-if succeeds, None of the remaining else, if’s or else’s, will be tested.
The if…else ladder (if…else…if) statement allows you to execute a code block among more than 2 alternatives.
The syntax of the if…else statement is:
if (condition) {
expressions A
} else if (condition) {
expressions B
} else if (condition) {
expressions C
} else {
expressions D
}
See the below example of the if-else ladder in R.
quantity <- 12
if (quantity < 0) {
print("It is not valid")
} else if (quantity == 6) {
print("It is half dozen")
} else if (quantity == 12) {
print("It is a dozen")
} else {
print("It is a more than dozen")
}
Output
[1] "It is a dozen"
If the quantity is negative, then if block will be executed. For example, if the quantity is 6, then the first else-if block is executed. If the quantity is 12, then the second else-if block is executed; otherwise, the last else block is executed.
Conclusion
To generalize, if-else in R programming needs three arguments:
- A statement (e.g., comparison operator) that evaluates to TRUE or FALSE.
- The value that R should return if the comparison operator is TRUE.
- The value that R should return if the comparison operator is FALSE.
That is it for this example.

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.