You are currently viewing R if…else with Examples

The if, if…else, and if…else…if in R are conditional decision-making statements that are used to execute a block of code based on a condition. Similar to other languages, in R, an if statement can be written in three ways; first, if statement without else, second if…else statement, and third if…else if statement.

Advertisements

Note that the else is an optional block in the if..else statement and when present else is executed when the if condition becomes FALSE.

Related:R ifelse() Function Usage with Examples

Points to note with if..else Statement

  • In R, the numeric value 1 is considered as TRUE and 0 is considered as FALSE.
  • R uses curly brackets { } to define the scope of the if statement.
  • If can have zero or one else and for if else if, it must come after any else if.
  • An if can have zero to many else if and they must come before the else.
  • When one if succeeds, none of the else or else if will be tested.
  • An if statement can return a value.

In this article, I will explain the following if statements in R.

Decision StatementsExplanation
ifAn if statement without an else block. Body of if is executed if condition is TRUE
if elseAn if statement with an optional else block. Else is executed when if condition is FALSE.
if else ifAn if statement with an optional else if block. You can chain any number of else if statements.
if else if else …Last else if executed when non of the prior if conditions TRUE.
if statements

1. R if Statement

The if keyword is used to write an if statement in R, and it is used to execute a block of code when a condition is TRUE. Since the else block is optional when an if condition becomes false the control goes to the next statement followed by the if block.


# R if statement
if(boolean_expression) {
  # R statements to execute if condition TRUE
}

Check the following flow of the if statement.

if in r

1.1. if Example

Following is an example of if in R.


# R if statement
str <- 'Spark'
if(str == 'Spark') {
  print('IF CONDITION IS TRUE')
}

# Output
# IF CONDITION IS TRUE

2. R if else Statement

The R if…else statement is used to execute a block of code when the if condition is TRUE and when it is FALSE it executes code from the else block. If you wanted to execute a block of code when the condition is FALSE, use the else block. Note that else is an optional block.

if else in r

2.1 if else Example

Following is an example of an if…else statement. Since we have str assigned with Python string, the condition becomes false, and the else block is executed.


# R if statement
str <- 'Python'
if(str == 'Spark') {
  print('IF CONDITION IS TRUE')
}else{
  print('IF CONDITION IS FALSE')
}

# Output
#IF CONDITION IS FALSE

3. R if else if Statement

The if…else…if is a third notation of the if statement in R. It can be written as if statement followed by an optional else if statement. You can write optional else if like a chain any number of times. If you wanted to execute a block of code when none of the conditions are TRUE, use the else block without if (if…else if…else).

if else if in r

3.1 if else if Example

The following is an example of if…else…if statement. Here, The else keyword catches anything which isn’t caught by the preceding conditions.


str <- 'Python'
if(str == 'Spark') {
  print('str value is Spark')
}else if(str == 'Python' ) {
  print('str value is Python')
}else{
  print('str value is not Spark and Python')
}

# Output
[1] "str value is Python"

Change the value of str to Spark and something else and validate how the output is changing.

4. R Nested if Statement

An if within another if is called a nested if statement in R. You can write if within if and within if and within if and so on. All the if scenarios we discussed above can be part of nested if. For example.


# Nested if statement
if (condition){
   if(condition){
      if(condition){

      }else if(condition){
          if(condition){

          }
      }
   }
}

5. Using with for Loop

Lastly, you can also use the if statement within loops in R (for, while, and repeat). Mainly when you want to exit the loop or skip the iteration based on a condition, you would use if statement.


# Using if with for
numbers <- c('One','Two',"Three","Four","Five")
for(i in numbers) {
  if( i == "Four")
    break
  print(i)
}

# Output
[1] "One"
[1] "Two"
[1] "Three"

Conclusion

In this article, you have learned various combinations of if statements in R. The if, if…else, and if…else…if in R are conditional decision-making statements that are used to execute a block of code based on a condition. Similar to other languages, in R, an if statement can be written in three ways; first, if statement without else, second if…else statement, and third if…else if statement.

Related Articles

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium