What is %in% operator?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

What is %in% operator?

What is %in% operator?

This recipe explains what is %in% operator

0

Recipe Objective

What is %in% operator? A %in% operator identifies whether an element is present in a given vector / Dataframe or not. It returns a logical output i.e TRUE value when the element is present or FALSE when not present. This recipe demonstrates an example of %in% operator.

Step 1- Define a vector a

a <- c(1:10) print(a)

Step 2 - Use the %in% operator

3 %in% a
 "Output of the code is TRUE"  
15 %in% a
 "Output of the code is FALSE"  

Step 3 - Define a dataframe

df <- data.frame(student_name = c('U','V','X','Y','Z'), grade = c('AA','CC','DD','AB','BB'), math_marks = c(40,80,38,97,65), eng_marks = c(95,78,36,41,25), sci_marks = c(56,25,36,87,15)) print(df)
 "Output of the code is" : 

  student_name grade math_marks eng_marks sci_marks
1            U    AA         40        95        56
2            V    CC         80        78        25
3            X    DD         38        36        36
4            Y    AB         97        41        87
5            Z    BB         65        25        15

Step 4 - Use the %in% operator for a dataframe

Use %in% operator to create a new variable in the dataframe and check the condition on garde as YES and NO

df1 = within (df,{ good_grade='NO' good_grade[grade %in% c('AA','AB','BB')]='YES' good_grade[grade %in% c('CC','DD')]='NO' }) print(df1)
 "Output of the code is" : 
  student_name grade math_marks eng_marks sci_marks good_grade
1            U    AA         40        95        56        YES
2            V    CC         80        78        25         NO
3            X    DD         38        36        36         NO
4            Y    AB         97        41        87        YES
5            Z    BB         65        25        15        YES

Relevant Projects

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.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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.

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.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

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

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.

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.