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

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

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.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

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.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT