Open In App

R If Else Conditions

The if-statement in Programming Language alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false? Here comes the R Programming Language else statement. We can use the else statement with the if statement to execute a block of code when the condition is false.

Syntax of if-else statement in R Language

if (condition) {
# code to be executed if condition is TRUE
} else {
# code to be executed if condition is FALSE
}

if-else statement in R

Working of if-else statements in R Programming

Flowchart if-else statement in R

R – if-else statement

Conditions and If Statements

Here we will show the use of logical conditions in if statements. Adjust the values and conditions as needed for our specific requirements.



Meaning Operator Example
Equal == x == y
Not equal != x != y
Greater than > a > b
Less than < x < y
Greater than or equal to >= x >= y
Less than or equal to <= x <= y

Ifelse Function in R Programming




x <- 5
  
# Check value is less than or greater than 10
if(x > 10)
{
    print(paste(x, "is greater than 10"))
} else
{
    print(paste(x, "is less than 10"))
}

Output 

[1] "5 is less than 10"

Here in the above code, Firstly, x is initialized to 5, then the if-condition is checked(x > 10), and it yields false. Flow enters the else block and prints the statement “5 is less than 10”.



Ifelse Function in R Programming




x <- 5
 
# Check if value is equal to 10
if(x == 10)
{
    print(paste(x, "is equal to 10"))
} else
{
    print(paste(x, "is not equal to 10"))
}

Output

[1] "5 is not equal to 10" 

Nested if-else statement in R

The if-else statements in R can be nested together to form a group of statements and evaluate expressions based on the conditions one by one, beginning from the outer condition to the inner one by one respectively. An if-else statement within another if-else statement in R better justifies the definition.

Syntax

if(condition1){
# execute only if condition 1 satisfies
if(condition 2){
# execute if both condition 1 and 2 satisfy
}
}else{
}

Ifelse Function in R Programming




# define a variable
x <- 15
 
# check the value of x using nested if-else statements
if (x < 10) {
  # if x is less than 10
  print("x is less than 10")
} else {
  # if x is greater than or equal to 10
  if (x < 20) {
    # if x is less than 20
    print("x is between 10 and 20")
  } else {
    # if x is greater than or equal to 20
    print("x is greater than or equal to 20")
  }
}

Output

[1] "x is between 10 and 20"

Ifelse Function in R Programming




# define variables for grades and income
grades <- 85
income <- 25000
 
# check eligibility for scholarship using nested if-else statements
if (grades >= 80) {
  # if grades are 80 or above
  if (income <= 30000) {
    # if income is 30,000 or less
    print("Congratulations, you are eligible for a scholarship!")
  } else {
    # if income is more than 30,000
    print("Sorry, your income is too high to qualify for a scholarship.")
  }
} else {
  # if grades are below 80
  print("Sorry, your grades are too low to qualify for a scholarship.")
}

Output

[1] "Congratulations, you are eligible for a scholarship!"

Article Tags :