If else in R || ifelse() in R programming language

If else in R || ifelse() in R programming language


Hello guys, what’s up? Today I am going to discuss you about if else in r. So let’s start—
Decision making is an important part of programming. I hope you guys know, there are many decision making statement. If-else is one of them. R programming language has a ifelse() build in function. At first I will teach you traditional if else r with example and then I will teach you ifelse() build in function R programming language.

if else statement with traditional way

In if statement, the inner block will be executed when the condition will be true. And the else block code will execute when if condition is false.

Syntax of If else in r
if (condition) {
    expression 1 
} else {
    expression 2
}
R programming language also support Boolean condition inside if else statement.
if(boolean_expression) {  
   // statement(s) will be executed if the boolean expression is true.  
} else {  
   // statement(s) will be executed if the boolean expression is false.  
}  
We can use nested if else statement in a program. We can also call it if…else ladder. Syntax is just like this:-
if(boolean_expression 1) {
   // Executes when the boolean expression 1 is true.
} else if( boolean_expression 2) {
   // Executes when the boolean expression 2 is true.
} else if( boolean_expression 3) {
   // Executes when the boolean expression 3 is true.
} else {
   // executes when none of the above condition is true.
}
Flow Chart of if else in r

If else in R || ifelse() in R programming language

I also show you another flow chart of if else statement for your clarifications.

If else in R || ifelse() in R programming language

Example1:

x <- c("what","is","truth")

if("Truth" %in% x) {
   print("Truth is found the first time")
} else if ("truth" %in% x) {
   print("truth is found the second time")
} else {
   print("No truth found")
}


Assume, our condition is satisfied with 2nd one. So, after executing this, our output just like below

[1] "truth is found the second time"

Example2: Assume you are fruit seller. If the customer buy fruits between 20 and 30, then it print Average day. If quantity is > 30 it will print “What a great day!”, otherwise “Not enough for today”. So your task is, write the code with if else r program.

# Create vector quantity
quantity <-  10
# Create multiple condition statement
if (quantity <20) {
      print('Not enough for today')
} else if (quantity > 20  &quantity <= 30) {
     print('Average day')
} else {
      print('What a great day!')
}

Assume, you have sold fruit less than 20, and then your output just looks like below.

## [1] "Not enough for today"


Example3:

# R if statement Example
 
# for TRUE condition
a = 6
 
if(a==6){
    print ("Condition a==6 is TRUE")
    print ("This is second statement in if block")
}
 
# for FALSE condition
a = 7
 
if(a==6){
    print ("Condition a==6 is TRUE")
    print ("This is second statement in if block")
} 

After executing this, our output just like below

 $ Rscript r_if_example.R 
[1] "Condition a==6 is TRUE"
[1] "This is second statement in if block"

Example4: According to the product purchased, VAT has different rate. Imagine we have 3 different kinds of products with different VAT like: 

If else in R || ifelse() in R programming language

So you task is to write a chain to apply the correct VAT rate to the product a customer bought. And one more thing, you have to write it in r programming language with if else statement. 

category <- 'A'
price <- 10
if (category =='A'){
  cat('A vat rate of 8% is applied.','The total price is',price *1.08)  
} else if (category =='B'){
    cat('A vat rate of 10% is applied.','The total price is',price *1.10)  
} else {
    cat('A vat rate of 20% is applied.','The total price is',price *1.20)  
}

If a customer purchased A category product, then your output looks like below.

# A vat rate of 8% is applied. The total price is 10.8


ifelse() in function R programming language

A powerful function to know about is ifelse(). You can write if statement within 1 line of code, which is shorthand function of traditional if else statement.

Syntax of ifelse() function

ifelse(test_expression, x, y)

In ifelse() function, test_expression is a condition. If the condition will true then ifelse() give the value of x, if the condition is false then ifelse() function show the value of y.
Let me clarify properly…..

ifelse(test, yes, no) 

I hope this one is more suitable to understand.

Example: ifelse() function

> a = c(5,7,2,9)
> ifelse(a %% 2 == 0,"even","odd")
[1] "odd"  "odd"  "even" "odd" 


The above example shows, whether values are odd or even which are containing in a variable.
So guys, that’s all about if else in r and iflse() . Later we will discuss another topic of r programming language. Till then, take care. Happy Coding

Post a Comment