What is %in% operator?

What is %in% operator?

What is %in% operator?

This recipe explains what is %in% operator

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

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

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.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

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.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

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.