How to Apply If Else Statements in R Programming (2 Examples)

In this R tutorial you’ll learn how to use different types of if and else statements.

Example 1: Combination of if() & else() R Functions

if("X" %in% LETTERS[1:10]) {             # If and else commands
  cat("X is in the first 10 letters")
} else {
  cat("X is NOT in the first 10 letters")
}
# X is NOT in the first 10 letters

Example 2: Using ifelse() Function

ifelse(test = "X" %in% LETTERS[1:10],    # ifelse function
       yes = "X is in the first 10 letters",
       no = "X is NOT in the first 10 letters")
# X is NOT in the first 10 letters

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top