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