Which is this operator %>% in R

what is the infix operator in R

Recipe Objective

"%>%" is an infix operator which is defined by magittr package and is heavily used by dplyr and tidyr packages. ​

It is mainly used to perform piping or chaining. Hence, the reference to Margritte's famous painting called "Treachery of Images". This concept is widely used when working with data especially when using dplyr and tidyr packages. Nesting is a usual way to perform multiple operations in one line but this gets complicated for the user to understand.

"%>%" operator increases the readability of the nested operation by using the concept of chaining. ​

In this recipe, we will demonstrate how "%>%" operator is used with a user defined function. ​

Example

# Data manipulation library(dplyr) x = c(1:10) y = 5 #defining a complex function with multiple operations in one line func = mean(sum((x-y)^2)) func
85
# using "%>%" to simplify the readability of the same defined function func_1 = (x-y)^2 %>% sum() %>% mean() func_1
85

Note: ​

  1. "%>%" operator can be read as "THEN" in this case. For example: first (x-y)^2 operation takes place THEN sum() operation and THEN mean() operation.
  2. It increases the readablity of the nested function by giving us the sequence if operation.

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

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.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

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.