How to write a if condition in R?

This recipe helps you write a if condition in R

Recipe Objective

How to write a if condition in R? Conditional statements control the flow of a program. if () statement is executed only when the condition returns a logical expression as TRUE. If the condition returns a FALSE value, then nothing is executed. if () statement can also be followed with an else statement which gets executed when the logical expression returns FALSE. This recipe demonstrates an example with If () and multiple if () statements.

Step 1- Create a vector

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

Step 2 - if statement

if ("hello" %in% x){ print("hello is present in x") }
"Output of the code is "hello is present in x"

Step 3- Multiple if statements

Multiple if statements can also be executed.Only those values whose condition is TRUE will be executed.

if ("hello" %in% x){ print("hello is present in x") } if (123 %in% x){ print("123 is present in x") } if ("hi" %in% x){ print("hi is present in x") }
"Output of the code is"
[1] "hello is present in x"
[1] "123 is present in x"
 

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

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.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.