Welcome to this tutorial on Pipe Operator.
In this tutorial,we will learn about:
summarise and group_by functions
Operations in summarise function
Pipe operator
To understand this tutorial, you should know,
Basics of statistics
Basics of ggplot2 and dplyr packages
Data frames
If not, please locate the relevant tutorials on R on this website.
This tutorial is recorded on
Ubuntu Linux OS version 16.04
R version 3.4.4
RStudio version 1.1.463
Install R version 3.2.0 or higher.
For this tutorial, we will use
A data frame moviesData.csv
A script file myPipe.R.
Please download these files from the Code files link of this tutorial.
I have downloaded and moved these files to pipeOps folder.
This folder is located in myProject folder on my Desktop.
I have also set pipeOps folder as my Working Directory.
Now we learn about summarise function.
summarise function reduces a data frame into a single row.
It gives summaries like mean, median, etc. of the variables available in the data frame.
We use summarise along with group_by function.
Let us switch to RStudio.
Open the script myPipe.R in RStudio.
Run this script by clicking on the Source button.
movies data frame opens in the Source window.
In the movies dataframe, scroll from left to right.
This will enable us to see the remaining objects of the movies data frame.
To know the mean of imdb_rating of all movies, we will use summarise function.
Click on the script myPipe.R
In the Source window, type the following command.
Inside the summarise function, the first argument is a data frame to be summarised.
Here, it is movies.
The second argument is the information we need, that is the mean of imdb_rating.
Save the script and run the current line by pressing Ctrl+Enter keys simultaneously.
The mean value is shown.
One will argue that I can find the mean by using mean function along with dollar operator.
What is the use of installing a whole package and using a complex function?
Basically, we do not use summarise function for computing such things.
This function is not useful unless we pair it with group by function.
When we use group_by function, the data frame gets divided into different groups.
Let us switch back to RStudio.
In the Source window, click on movies data frame.
In the movies data frame, scroll from right to left.
We will group the movies data frame based on the genre.
For this, we will use group underscore by function.
Click on the script myPipe.R
In the Source window, type the following command.
Run the current line.
A new data frame groupMovies is stored.
Now, we will use summarise function on this data frame.
In the Source window, type the following command.
Run the current line.
I will resize the Console window
The mean values of all movies in different genres are displayed.
Notice that, Documentary genre has the highest mean imdb_rating.
And Comedy genre has the lowest mean imdb_rating.
I will resize the Console window
In the Source window, click on movies data frame.
In the movies data frame, scroll from left to right.
Let us find the mean imdb_rating distribution for the movies of Drama genre.
Also, we will group movies of Drama genre by mpaa_rating.
For this, we will use filter, group_by, and summarise functions one by one.
Click on the script myPipe.R
In the Source window, type the following commands.
First, we will extract the movies of Drama genre.
Then, we group these movies based on mpaa_rating.
Finally, we apply summarise function.
This will calculate the mean of the filtered and grouped movies.
Run the last three lines of code.
I will resize the Console window
The required mean values are printed on the console.
I will resize the Console window again.
In this code, we have to give names to each and every intermediate data frame.
But there is an alternate method to write these statements using the pipe operator.
The pipe operator is denoted as %>%.
It prevents us from making unnecessary data frames.
We can read the pipe as a series of imperative statements.
If we have to find the cosine of sine of pi, we can write
Let us switch to RStudio.
We will learn how to do the same analysis by using the pipe operator.
In the Source window, type the following command.
Here three lines of code have been written as a series of statements.
We can read this code as,
Using the movies data frame, filter the movies of Drama genre
Next, group the filtered movies by mpaa_rating
Finally, summarise the mean of imdb_rating of the grouped data.
This code is easier to read and write than the previous one.
In the case of pipe operator, we don’t have to repeat the name of the data frame.
Notice that we have written name of the data frame only once.
Save the script and run the current line.
I will resize the Console window.
The required mean values are printed on the Console.
I will resize the Console window again.
In the Source window, click on movies data frame.
In the Source window, scroll from left to right.
Let us check what is the difference between critics_score and audience_score of all the movies.
We will use a box plot for our study.
By using the pipe operator, we can combine the functions of ggplot2 and dplyr packages.
Click on the script myPipe.R
In the Source window, type the following command.
Save the script and run the current line.
The required box plot appears in the Plots window.
In the Plots window, click on the Zoom button to maximize the plot.
Here you can see that for the genres Drama, Horror, and Mystery & Suspense movies, the median is close to zero.
This means that the audience and critics opinions are very similar for these genres.
Whereas for Action & adventure and Comedy movies, the median is not close to zero.
This means that the audience and critics opinions are different for these genres.
Close this plot.
In the Source window, click on movies data frame.
In the Source window, scroll from right to left.
Let us check the number of movies in every category of mpaa_rating of each genre.
Click on the script myPipe.R
In the Source window, type the following command.
Notice that we have included both genre and mpaa_rating in group_by function.
So, the analysis will be done on the data divided by these 2 variables.
Also, we have used num = n().
The function n computes the number of times the event with specific condition has happened.
Run the current line.
I will resize the Console window.
From the output, we can see that there are 22 Action and Adventure movies with mpaa_rating as R.
Let us summarize what we have learnt.
In this tutorial, we have learnt about:
summarise and group_by functions
Operations in summarise function
Pipe operator
We now suggest an assignment.
Use the built-in data set iris.
Using the pipe operator, group the flowers by their species.
Summarise the grouped data by the mean of Sepal.Length and Sepal.Width.
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it.
We conduct workshops using Spoken Tutorials and give certificates.
Please contact us.
Please post your timed queries in this forum.
Please post your general queries in this forum.
The FOSSEE team coordinates the TBC project.
For more details, please visit these sites.
The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
The script for this tutorial was contributed by Varshit Dubey (CoE Pune).
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.