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

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

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.

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.

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.

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

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.