How to write a if condition in R?

How to write a if condition in R?

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"

Relevant Projects

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

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.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

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.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.