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.
- #s1 is the marks of the student in subject 1
- s1<-45
- if(s1>40){
- print("PASS")
- }else{
- print("FAIL")
- }
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.
- s1<-45
- s2<-80
- s3<-30
- if(s1>40 & s2>40 & s3>40){
- print("PASS")
- }else{
- print("FAIL")
- }
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.
- x=40
- y=31
- z=20
- if(x>y){
- print("x is greater than y")
- if(x>z){
- print("x is greater than z too")
- }
- }
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.
- marks<-80
- if(marks>75){
- print("GOOD")
- }else if(marks>40 & marks<=75){
- print("AVERAGE")
- }else{
- print("POOR")
- }
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.
- x<-2
- if(x%%2==0){
- print("Even")
- }else{
- print("Odd")
- }
To check whether a number is divisible by another number.
- dividend<-25
- divisor<-3
- quotient<-dividend%/%divisor
- remainder<-dividend%%divisor
- if(remainder==0){
- print("Divisible")
- }else{
- print("Not Divisible")
- }
To check whether a number is positive or negative.
- num = 10
- if(num<0){
- print("Num is NEGATIVE")
- }else if(num>0){
- print("Num is POSITIVE")
- }else{
- print("Num is ZERO")
- }
Try out the examples by changing the values of the variables.
Stay tuned. More articles in this series are coming soon.
Happy Learning!