If else in R: How to Use if-else Statement

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. 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 sections of code are executed based on defined parameters.

Flow chart of if-else in R

If else in R: How to Use if-else Statement

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 try 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 Ladder

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.

  1. If statement can have zero or one else, and it must come after any else if’s.
  2. If statement can have zero to many else if’s and they must come before the else.
  3. 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 block of code 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. 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:

  1. A statement (e.g. comparison operator) that evaluates to TRUE or FALSE.
  2. The value that R should return if the comparison operator is TRUE.
  3. The value that R should return if the comparison operator is FALSE.

That is it for if else in R.

Leave a Comment