How to write if and else if in R?

How to write if and else if in R?

How to write if and else if in R?

This recipe helps you write if and else if in R


Recipe Objective

Conditional statements control the flow of the program. There are some cases where you need to execute a function only when a certain condition is met and some different function if it is not. We can carry out this task using if...else statements in R.

Nested if..else if...else statements is one type of statement where you check multiple conditions and execute the functions accordingly. This recipe demonstrates how to use the nested if..else statements.


if (boolean_expression1) { // statements you want to execute if the if the boolean expression1 is TRUE } else if (boolean_expression2) { // statements you want to execture if the boolean_expression2 is TRUE } else { // statements you want to execute if all the above conditions are False }

Example: We will check different elements present in the vector using nested if..else statement

Step 1: Create a numeric vector

vec_ = c("Rome", "Mumbai", "NewYork", "Edinburgh")

Step 2: Nested if..else if..else

We will use %in% operator in the boolean expression to check the elements present in the vector

if ("Delhi" %in% vec_){ print("Delhi is present in the vector") } else if ("NewYork" %in% vec_){ print("New York is present in the vector ") } else { print("New York and Delhi not present in the vector ") }
[1] "New York is present in the vector "

Relevant Projects

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.