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 CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.