How to write if else in R?

This recipe helps you write if else in R

Recipe Objective

How to write a if else condition in R ? Conditional statements control the flow of the program. In if-else condition, the if () statement is executed when the condition is TRUE. If the expression returns a FALSE value, then the else() statement is executed. This recipe demonstrates an example of an if-else statement.

Sentiment Analysis Project on eCommerce Product Reviews with Source Code

Step 1- Create a vector

x <- c(list(123,2.5,"hello"))

Step 2 - if else statement

if ("hi" %in% x){ print("hi present in x") } else { print("hi not present in x") }

"Output of the code is hi not present in x"  

Step 3- Nested if-else statement

Nested if-else statement executes only that statement which returns TRUE value.

if (13 %in% x){ print("13 is present in x") } else if (123 %in% x){ print("123 is present in x") } else{ print("not present in x") }

 "Output of the code is 123 is present in x" 

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

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.

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.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .