If-else statement in R

In R, you can use the if-else statement to create conditional logic. This allows you to execute different codes depending on whether a specified condition is true or false, or simply if the condition is met or not. The if condition can be either used separately or in combination with the else statement, to specify certain conditions.

To further understand the if and else statement in R, let’s create a variable using the following command

Download Example File
marks <-53 

A variable named marks, contains the marks of a student i.e. 53 marks.

We want to evaluation a condition that if a student has secured 50 or higher marks, if that is the case then we would want to print the word “Pass”.

if(marks >= 50) {
   print("Pass")
} 

The above command checks that whether the variable “marks” is greater than or equal to 50, and if the condition is true, the marks variable is greater than 50, the command prints “Pass.” The output of above command will be “Pass” because the condition is actually met here.

What could happen if the condition is not met? For instance, if instead of setting marks >50 in the above command, we want to check whether marks are greater than 60, then the following command would be used.

if(marks >= 60) {
   print("Pass")
}

Just like the previous command, we want to check whether marks are greater than 60 or not. If the marks are greater than 60, it would print Pass, but if the marks are not greater than 60, it would not print anything. As we know, marks are set to 53 and are less than 60, so the command would not print Pass or print anything at all. It would only print anything when the if condition is met.

Comparison Operators

There are certain comparison operators that are used along with if statement. These operators are different and hence serve different purposes.

The first one is equality operator “==“. It is used to check if two values are equal. In an if statement, we can use the equality operator to compare a variable to a specific value to check if they are the same. The following command will be executed to use equality operator.

if (marks == 53) { 
    print("Marks is exactly 53")
}

The above command specifies that if marks are equal to 53, then print the given statement. Any other code than “Marks is exactly 53” can also be executed by writing it in inverted commas in the command.

There are greater than “>” and less than “<” operators too, that can be used with an if statement. If we want to check whether the marks variable is greater than a specified threshold, we use the following command.

if (marks> 35) {
    print( "marks are greater than 35")
}

Similarly, the less than operator will be used in the following way

if (marks < 53) {
    print( "marks are less than 53")
}

The other two operators, greater than or equal to and less than or equal to, are somewhat used in a similar way as used in the commands above. One example of using the greater than or equal to “>=” is as following

if (marks >= 40) {
    print("Marks are 40 or greater")
}

Similarly, the following command will check if marks are less than or equal to 53.

if (marks <= 53) {
    print("Marks are 53 or less")
}

If-Else Statement

As mentioned earlier, the If statement can be used along with the “else statement”. How does this work? In a given command, the if statement checks a condition. If the condition is true, a specific code inside the if is executed. If the condition is false, the code inside the if is skipped and the code inside the else statement will be executed. So else is a fail-safe when condition is not met.

For instance, take a look at the following command

if(marks >= 50) {
   print("Pass")
} else {
   print("Fail")
}

In the above command, if marks are greater or equal to 50, the Pass should be printed. If, however, the condition isn’t met and the marks are not equal to or greater than 50, the Fail should be printed. Given that marks are greater than 50, the output Pass will be printed.

Multiple conditions can be used in the if statement by using the AND logical operator (&). For instance, if your requirements say that students need to have marks above or equal to 50% and attendance above or equal to 80% to pass. Let’s first create a variable named attendance by using the following command

attendance <- 40

Then to write both conditions of marks and attendance, we use the following command

if(marks>=50 & attendance>=80) {
   print("Pass")
   } else {
   print("Fail")
}

For the code inside the if block; Pass, to be executed, both of the above conditions must be met. If either of the conditions is false, the code inside the else block will execute. Given that attendance is 40%, both conditions are not met, so code in the else statement will be printed.

Another way to use an if-else statement is by writing both statements together in a command, the following way.

ifelse(marks>50,"Pass","Fail")

So in above command, the condition is marks > 50, which checks if the value of the marks variable is greater than 50. If the condition is true, i.e., marks is greater than 50, command returns “Pass.” If the condition is false, it returns “Fail.

We have just checked the if-else statement to be true or false till now. If, however, we want to assign the output to a new variable, we can use the following command.

marks <- 81

if(marks >50) {
   result <- "pass"
}

In this command, if the marks are greater than 50, the result variable is assigned the value “pass.”

Else-if Statement or Chaining If Statement

Like If and else statement, the else if statement is used in combination with the if statement to create a series of conditions. It allows us to check multiple conditions one by one until one of them is true, and then execute the corresponding code. Now, if we want to use if-else statement to check certain conditions, for the grading of students, we can use the following command.

marks <- 81 

if (marks > 90) {
    print("A Grade")
} else if(marks > 70){
    print("B Grade")
} else if(marks > 50) {
     print("C Grade")
} else {
     print("Fail")
}

In the above command, the command starts by checking if the marks are greater than 90. If this condition is met, it assigns an “A Grade” to the student. If not, it moves on to the next else if statement, which checks if the marks are greater than 70. In the second line of code, if this condition is true, the code assigns a “B Grade.” If the marks are neither greater than 90 nor 70, it proceeds to the final else if statement, which checks if the marks are greater than 50. If this condition is satisfied, the code assigns a “C Grade.”

However, if the student’s marks do not meet any of the above conditions (i.e. if the marks are 50 or less), the code falls into the else block and prints “Fail” to indicate that the student has not passed. In this specific case, with marks set to 81, the student is given a “B Grade.

This process is also called chaining if statements because we are chaining multiple if statements to create a series of conditions to be checked one after another.

Nesting if Statement

Nesting the if statement is similar to chaining the if statement in a way that they both somehow serve the same purpose, but nesting if is different from chaining if statement in terms of readability and execution. Unlike chaining if statement, nesting if statements have if statements inside if or else blocks, creating a hierarchy or nesting of conditions. For example, for the same grades as used above, the command for nesting if statement will be as following

marks <- 81 

if (marks > 90){
    print("A Grade")
}   else {
    if(marks > 70) {
       print("B Grade")
    }  else {
       if(marks > 50) {
          print("C Grade")
        } else {
          print("Fail")
}}}

The output will be same as the chaining if statement. However, it can make the code harder to read and understand, especially as the level of nesting increases, so usage of chaining if statement is easy to read and efficient.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Tweet
Share
Share
Pin