How to write if else in R?

How to write if else in R?

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

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" 

Relevant Projects

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.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

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.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

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.

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.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.