R if-else Statement

Introduction

In this chapter, you will learn about the if-else statement in R. The if-else statement is a fundamental control structure that allows you to execute different blocks of code based on whether a condition is TRUE or FALSE. Understanding how to use the if-else statement is essential for making decisions in your R programs.

The if-else Statement

Basic if Statement

The if statement executes a block of code if a specified condition is TRUE.

Syntax:

if (condition) {
  # Code to execute if condition is TRUE
}

Example:

# Example of a basic if statement
x <- 10

if (x > 5) {
  print("x is greater than 5")
}
# Output: [1] "x is greater than 5"

if-else Statement

The if-else statement allows you to execute one block of code if the condition is TRUE and another block of code if the condition is FALSE.

Syntax:

if (condition) {
  # Code to execute if condition is TRUE
} else {
  # Code to execute if condition is FALSE
}

Example:

# Example of an if-else statement
x <- 3

if (x > 5) {
  print("x is greater than 5")
} else {
  print("x is not greater than 5")
}
# Output: [1] "x is not greater than 5"

if-else if-else Statement

The if-else if-else statement allows you to test multiple conditions sequentially. The first condition that evaluates to TRUE will have its corresponding block of code executed.

Syntax:

if (condition1) {
  # Code to execute if condition1 is TRUE
} else if (condition2) {
  # Code to execute if condition2 is TRUE
} else {
  # Code to execute if none of the conditions are TRUE
}

Example:

# Example of an if-else if-else statement
x <- 7

if (x > 10) {
  print("x is greater than 10")
} else if (x > 5) {
  print("x is greater than 5 but less than or equal to 10")
} else {
  print("x is 5 or less")
}
# Output: [1] "x is greater than 5 but less than or equal to 10"

Nested if-else Statements

You can nest if-else statements within each other to check multiple conditions in a more complex decision structure.

Example:

# Example of nested if-else statements
x <- 8

if (x > 0) {
  if (x %% 2 == 0) {
    print("x is a positive even number")
  } else {
    print("x is a positive odd number")
  }
} else {
  print("x is not a positive number")
}
# Output: [1] "x is a positive even number"

Vectorized if-else with ifelse()

The ifelse() function is used to perform vectorized conditional operations. It applies the if-else logic to each element of a vector.

Syntax:

ifelse(test, yes, no)
  • test: A logical condition.
  • yes: The value to return for TRUE elements of test.
  • no: The value to return for FALSE elements of test.

Example:

# Example of ifelse function
x <- c(5, 10, 15)
result <- ifelse(x > 10, "Greater than 10", "10 or less")
print(result)
# Output: [1] "10 or less"      "10 or less"      "Greater than 10"

Example Program with if-else Statements

Here is an example program that demonstrates the use of if-else statements in R:

# R Program to Demonstrate if-else Statements

# Declare a variable
score <- 85

# Determine the grade based on the score
if (score >= 90) {
  grade <- "A"
} else if (score >= 80) {
  grade <- "B"
} else if (score >= 70) {
  grade <- "C"
} else if (score >= 60) {
  grade <- "D"
} else {
  grade <- "F"
}

# Print the grade
print(paste("The grade is:", grade))
# Output: [1] "The grade is: B"

# Use of ifelse() for vectorized operations
scores <- c(95, 82, 67, 58, 74)
grades <- ifelse(scores >= 90, "A",
                 ifelse(scores >= 80, "B",
                        ifelse(scores >= 70, "C",
                               ifelse(scores >= 60, "D", "F"))))
print(grades)
# Output: [1] "A" "B" "C" "F" "C"

Conclusion

In this chapter, you learned about the if-else statement in R, including the basic if statement, the if-else statement, the if-else if-else statement, nested if-else statements, and the vectorized ifelse() function. These control structures are essential for making decisions and controlling the flow of your R programs. By mastering these statements, you can write more dynamic and responsive R code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top