EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial Else if in R
Secondary Sidebar
R programming Tutorial
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

Else if in R

By Priya PedamkarPriya Pedamkar

else if in r

Introduction to Else if in R

A conditional statement that is used to validate a certain condition after the condition is proved false in the IF statement and subsequently gets passed to it in the sequence of conditional statements, returning a False by it to passes the control either to the next else if in the conditional block, or to else if no more else if is thereafter it, and executes successfully all the statements under it if the condition is proved true, with statement resembling else if statement structure of several other programming languages, is referred to as else if in R programming.

Syntax:

  • Else if in R language is reserved keywords and it resides inside the if-else block and processes the associated expression based upon the conditions.
  • Following is the else if syntax in R language:

if (condition){
# expression
} else if (condition){
# expression
} else if (condition){
# expression
} else {
# statement
}

  • Condition is written inside () and the expression is written inside { }, the number and position of opening and closing parenthesis determines the scope of the else if and if statements.
  • The number of else if block depends upon the programming logic or subroutine implementations.
  • Also, the number of expressions or logic inside an else if block may vary depending upon business logic requirements.
  • The else if block supports nested or multiple sets of if statement followed by else if statement inside its scope based upon the programming logic.
  • In some cases else block is an optional part.
  • It is a programming best practice to put default expressions in the else block which increases the program efficiency and code completeness.

Flowchart

  • The flow chart shown in the below image contains one else if block which is associated with if statement block and else statement block.
  • The ‘else if’ and else keyword is represented in green color in the flowchart diagram.
  • The condition is represented in blue color which evaluated as the TRUE or FALSE and routes to Yes or No direction.
  • The expression is replanted as gray color which indicates the processor including single or a group of statements.

flowchart

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

How Else if Statement Works in R?

1. The if-else and else if keywords allow associated with conditions for evaluation.

2. The condition return TRUE or FALSE value based upon the condition statement.

3. When the R program starts checking line by line in the code in the run time:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,171 ratings)
  • It first checks and evaluates the return value of the condition present with the If statement.
  • If the condition returns a TRUE Boolean value, R routed inside the if block and process for the expression or programming logic written in that block.
  • If it founds the FALSE Boolean value, it will not go inside the If block, rather it will check for next available else if block.
  • And program control moves to the else if statement if it found in the sequence of if-else if syntax.
  • In the next step, it will check and evaluate the condition statement associated with the else if block.
  • If the condition statements associated with else if block evaluated to return a TRUE boolean value, R process the code available inside the else if block.
  • However, if the condition of else it returns a FALSE boolean value, R ignores the code and checks for next else if statement.
  • If it founds another else if block subsequently, it processes the else if block in the same way.
  • This iteration goes on until all the available else if block found for the if block scope.
  • In the event of R runtime not found any else if block, it will search for else statement and process the code associated with the else block. And completes the IF else checking logic.
  • And in the event, if R runtime not found any else if or else block it completes if-else checking logic immediately after the if block scope.

Examples of Else if in R

Given below are examples using R studio IDE with R terminal.

Business Scenario

There are four inventory bins present in a manufacturing company, Items are stored in multiple bins based upon the Item Number identification, R program will assign the Bin Items to vectors and implement else if logic to find a particular item Bin and display the respective bin information. This R code will represent the inventory bin tracking for an Item.

We will declare assign values 4 vectors in R code as follows:

R Code:

# Assign vector values
Inventory_bin_A <- c("Item1","Item2","Item8","Item42")
Inventory_bin_B <- c("Item28","Item74","Item86","Item6","Item48")
Inventory_bin_C <- c("Item258","Item64","Item96","Item62","Item18")
Inventory_bin_D <- c("Item23","Item86","Item31","Item66","Item90")

  • We will print the vectors in R studio to check the assignment happened correctly on the console.

R code:

#print the vectors
print(Inventory_bin_A)
print(Inventory_bin_B)
print(Inventory_bin_C)
print(Inventory_bin_D)

Output:

else if in r 1

  • In the next step, we will implement else if statement in R to check ‘Item86’ belongs to which inventory bin.

R Code:

# writing else if logic to search Item86 #
if("Item86" %in% Inventory_bin_A){
print('Item86 found on Inventory_bin_A')
} else if("Item86" %in% Inventory_bin_B){
print('Item86 found on Inventory_bin_B')
} else if("Item86" %in% Inventory_bin_C){
print('Item86 found on Inventory_bin_C')
} else if("Item86" %in% Inventory_bin_D) {
print('Item86 found on Inventory_bin_D')
} else{
print('Item86 not available in the inventory bins')
}

Output:

else if in r 2

  • The R console output shows Item86 found on Inventory_bin_B.
  • As you observed in the assignment output Inventory, Item86 preset in two bins:
  1. Inventory_bin_B
  2. Inventory_bin_D
  • Based upon the example R check the TRUE Boolean return value from the condition, Once it gets 1st occurrence of the TRUE condition, it does not check for further else if condition.
  • Next, we will remove Item86 from bin Inventory_bin_B and run the code again.

R Code:

# Removing Item86 from Bin B
Inventory_bin_B<-Inventory_bin_B[!Inventory_bin_B %in% c("Item86")] print(Inventory_bin_B)

Output:

else if in r 3

  • Now the Item86 is not available in Inventory_bin_B
  • We will run the else if code again and check the output

Output:

Found on Inventory

This R console output shows the updated Inventory ‘Inventory_bin_D’ bin information through the else if code.

Conclusion

Else if in R language is one of the handy programming features that can be implemented in several places like inside a loop or outside a loop condition. It helps the R programmers to write simplified logic and provides better code blocks development. The else if block increase code readability.

Recommended Articles

This has been a guide to Else if in R. Here we discuss the syntax, how Else if Statement Works in R?and examples. You may also have a look at the following articles to learn more –

  1. IF-Else Statement in Matlab
  2. If Else Statement in Python
  3. Hill Climbing in Artificial Intelligence
  4. If-else Statement in C
  5. if else Statement in PHP | How to Work?
  6. Guide to Examples of Vectors in JavaScript
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more