How to write a if condition in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

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 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.

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.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Churn Prediction in Telecom using Machine Learning in R
Estimating churners before they discontinue using a product or service is extremely important. In this ML project, you will develop a churn prediction model in telecom to predict customers who are most likely subject to churn.

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.

Machine learning for Retail Price Recommendation with Python
Use the Mercari Dataset with dynamic pricing to build a price recommendation algorithm using machine learning in Python to automatically suggest the right product prices.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

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.