If Else in R – A Tutorial

In this tutorial we’ll look at if..else in R conditional statements

We will start with introduction to conditional statements and then cover different versions of an if statement in R.  The different versions are: the simple if statement, if-else statement, if else function and nested if-else statement. The concepts are similar to R programming.

Conditional Statements are very  useful in data science programming when we want to execute code only if a certain condition is satisfied. The condition is referred as a logical expression and is evaluated as either  true or false. The statements are executed based on the evaluation true/false.The simplest form is if statement. else  and else if  statements can also be used depending on the number of conditions to be checked.

if else R

The if statement executes a block of code if a specific condition is true. If the condition is false then the block of code is not executed.

 Syntax:
 
 if Boolean expression: 
  statement(s) 
if else condition

The first example here evaluates whether the value of object x is a positive number. If the condition x > 0 is true then the message “Positive number” is printed.

If the value of x is less the 0 or the  condition is false then the message is not printed. The second example prints the object value only if the object type is string or character.

 x = 5
 if x > 0:
     print("Positive number") 
 Positive number 
 x = "Hello there"
 if isinstance(x,str):
     print(x) 
 Hello there 

The R if-else statement executes a block of code if a specific condition is true. If the condition is false then another block of code can be executed.

 R if else Syntax:
 
 if Boolean expression: 
     statement(s) 
           else: 
     statement(s) 
R if else syntax

Here again we are evaluating whether the value of object x is a positive number. If the condition of  x > 0 is true then the message “Positive number” is printed.

If the condition is false then the message “Negative number” is printed.

 x = -5
 if x > 0:
     print('Positive number')
 else:
     print('Negative number') 
 Negative number 

Here’s a different example where we are taking user input using the  input function. The input must be integer value. If the value is less then zero then a message “Enter a positive number” is printed.  If the condition is false then sum of natural numbers up to the user input is printed. The user’s input of 5 prints “The sum is 15”.

 num = int(input("Enter a number: "))
 if num < 0:
     print("Enter a positive number")
 else: 
     sum=(num * (num + 1)) / 2
     print("The sum is", sum) 
 Enter a number: 5
 The sum is 15.0 

In this example, we are checking if the number is even or odd.

The loop variable x takes values for each element of the object a and  evaluates if it is even or odd and then prints the results as “even” or “odd”

 a = [6,1,5,14]
 print([("even" if x%2 == 0 else "odd") for x in a]) 
 ['even', 'odd', 'odd', 'even'] 

The R if-else statement can be made more complex using nested if-else statement. The nested  if-else statement executes a block of code if a specific condition is true. If the condition is false then another block of elif statement is executed.

 Syntax:
 
   if Boolean expression1: 
       statement(s) 
   elif Boolean expression2: 
       statement(s) 
   elif Boolean expression3: 
       statement(s) 
   else: 
       statement(s) 

This is how nested if-else statements are executed. Here we have an if condition followed by elif condition and else statements. The elif statement is executed only when the if condition is false. The body of the else is executed only when elif condition is false.

elif statement

In this example, if the condition x > 0 is TRUE then the message “positive number” is printed. If the condition is false then el-if statement checks for condition x =0 . If TRUE message “zero” is printed . If FALSE then “negative number” is printed.

 x=-11
 if x>0:
     print('x is a positive number')
 elif x==0:
     print('x is zero')
 else:
     print('x is a negative number') 
 x is a negative number 

This is a quick recap of concepts in the video.  We covered if ,if-else and nested if-else statements.

R if else summary

This tutorial lesson is taken from the Postgraduate Diploma in Data Science.