If Else Statement In R

In my previous article, I gave an overview of variables and data types in R along with some simple examples. If you are not aware of variables and data types in R, then I recommend you to read "Variables And Data Types in R". If you are completely new to R language, then please read "Introduction to R and RStudio". This article is about the conditional statement IF-ELSE statement in R.
 

IF-ELSE Statement 

 
The If-Else statement is used when we need to perform a particular task based on a condition. The If block executes if the condition is true else the Else block is executed.
 
Let us take a real-life simple example of a mark sheet and its calculations. Suppose that the passing marks are 40 and the student needs to pass in all the papers to pass. Here, the condition is that the marks should be greater than 40 in order to pass. Else, the student fails.
 
Run the following code snippet and observe the output. 
  1. #s1 is the marks of the student in subject 1
  2. s1<-45  
  3. if(s1>40){  
  4.   print("PASS")  
  5. }else{  
  6.   print("FAIL")  
  7. }  
When you run the above code, you must get "PASS" in the output because the If condition is satisfied. 
 
Change the value of s1 to some value below 40 and run the code. You must get the output as "FAIL". 
 
Important Note
If you observed this in the code snippet above, the else block starts immediately after the ending bracket of the if block. Try writing the else block from the next line and you will get an error when you execute the code.
 
In the above example, I considered marks of only one subject. In the real world, there would be many subjects and the student must get above 40 in all of them to pass. Even if he/she fails in one subject, the final result should be "FAIL".
 
Suppose there are three subjects and the student must get above 40 in all the 3 subjects to pass. The R script for this scenario will look like below.
  1. s1<-45  
  2. s2<-80  
  3. s3<-30  
  4. if(s1>40 & s2>40 & s3>40){  
  5.   print("PASS")  
  6. }else{  
  7.   print("FAIL")  
  8. }  
The above code snippet uses the logical AND (&) operator in the condition to evaluate whether the scores in all the 3 subjects are greater than 40. The if condition here is a combination of 3 conditions: s1>40, s2> 40 and s3>40. Only if the individual three conditions turn out to be true, the if block will be executed. Even if a single condition turns out to be false, the else block would be executed. That is the power of "&" operator.
 

NESTED IF Statement

 
At times, we will have to write if statements within if statements. If one if block is executed then the if control within it should be evaluated. Such nesting of if within another if is known as a Nested If statement.
  1. x=40  
  2. y=31  
  3. z=20  
  4. if(x>y){  
  5.   print("x is greater than y")  
  6.   if(x>z){  
  7.     print("x is greater than z too")  
  8.   }  
  9. }  
Run the above code snippet. Try changing the values of x,y,z and run it. Observe the output every time you run it and try to understand the logic.
 

ELSE IF Statement

 
In the above examples, we saw the if-else statement and nested if statements. There might be a situation where a condition needs to be evaluated in the else block. Else if statement helps in specifying such condition.
  1. marks<-80  
  2. if(marks>75){  
  3.   print("GOOD")  
  4. }else if(marks>40 & marks<=75){  
  5.   print("AVERAGE")  
  6. }else{  
  7.   print("POOR")  
  8. }  
In the above example, if the marks are above 75 then GOOD is printed. But if the marks are between 40 and 75 then, AVERAGE is printed.
 
For this, we have used else if statement. Finally, we have used else statement for printing POOR if the marks are lesser than 40.
 
Change the value of marks to 30 and run the code. You must get POOR as output. 
 
Change the value of marks to 50 and run the code. You must get AVERAGE as output.
 

Few examples of IF ELSE in R 

 
To check whether a number is even or odd.
  1. x<-2  
  2. if(x%%2==0){  
  3.   print("Even")  
  4. }else{  
  5.   print("Odd")  
  6. }  
To check whether a number is divisible by another number.
  1. dividend<-25  
  2. divisor<-3  
  3. quotient<-dividend%/%divisor  
  4. remainder<-dividend%%divisor  
  5. if(remainder==0){  
  6. print("Divisible")  
  7. }else{  
  8. print("Not Divisible")  
  9. }  
To check whether a number is positive or negative.
  1. num = 10  
  2. if(num<0){  
  3.   print("Num is NEGATIVE")  
  4. }else if(num>0){  
  5.   print("Num is POSITIVE")  
  6. }else{  
  7.   print("Num is ZERO")  
  8. }  
Try out the examples by changing the values of the variables. 
 
Stay tuned. More articles in this series are coming soon.
 
Happy Learning! 


Similar Articles