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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy