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.

Sentiment Analysis Project on eCommerce Product Reviews with Source Code

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.

Syntax:

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 "

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud