What is %in% operator in R

This recipe explains what is %in% operator in R

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.

Learn to Implement Deep Learning Techniques for Medical Image Segmentation

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

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

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.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

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.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.