Welcome to this tutorial on Data manipulation using dplyr package.
In this tutorial, we will learn about,
Data manipulation
dplyr package
How to use filter and arrange functions
To understand this tutorial, you should know,
Basics of Statistics
Basics of ggplot2 package
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
And A script file myVis.R.
Please download these files from the Code files link of this tutorial.
I have downloaded and moved these files to DataVis folder.
This folder is located in myProject folder on my Desktop.
I have also set DataVis folder as my Working Directory.
In real life, it is rare that we get the data in exactly the right form we need.
Often we’ll need to
create some new variables or summaries
rename the variables
reorder the observations in order to make the data a little easier to work with.
We will learn how to achieve all this by using dplyr package.
dplyr is a package for data manipulation, written and maintained by Hadley Wickham.
It comprises many functions that perform mostly used data manipulation operations.
Let us switch to RStudio.
Open the script myVis.R in RStudio.
Let us run this script by clicking on the Source button.
movies data frame opens in the Source window.
This data frame will be used later in this tutorial.
Now, we will install dplyr package. Please make sure that you are connected to the Internet.
In the Console window, type the following command and press Enter.
The installation of the package takes a few seconds.
We will wait while the package is being installed.
To load this package, we will add the library at the top of the script.
Click on the script myVis.R
At the top of the script, type library and dplyr in parentheses.
Save the script and run this line by pressing Ctrl + Enter keys simultaneously.
Now we learn about some key functions in dplyr package:
filter- to select cases based on their values.
arrange - to reorder the cases.
select - to select variables based on their names.
mutate - to add new variables that are functions of existing variables.
summarise - to condense multiple values to a single value.
All these functions can be combined with group underscore by function.
It allows us to perform any operation by a group.
Let us switch to RStudio.
In the Source window, click on movies.
In the Source window, scroll from left to right.
This will enable us to see the remaining objects of movies data frame.
Suppose we want to filter the movies having genre as Comedy.
For this, we will use the filter function.
Click on the script myVis.R
In the Source window, type the following command.
Recall that, filter function in dplyr package allows us to select cases based on their values.
Inside the filter function, the first argument is the name of the data frame which is movies.
The second argument is the value by which we want to filter the movies data frame.
Save the script and run the current line.
Resulting data frame is stored in an object called moviesComedy in the Environment window.
Let us view the data frame moviesComedy to check whether it contains movies with genre as Comedy.
In the Source window, type the following command.
Run the current line.
moviesComedy data frame opens in the Source window.
All the movies having genre as Comedy have been filtered.
Let us close this data frame moviesComedy for now.
We can also use logical operators to combine two or more than two values.
In the Source window, click on movies.
Suppose we want to filter the movies with genre as either Comedy or Drama.
Click on the script myVis.R
In the Source window, type the following commands.
Here, we have two values by which we would like to filter movies data frame.
For this, we have used a logical OR operator.
Run the last two lines of code.
moviesComDr opens in the Source window.
The movies having genre as either Comedy or Drama have been filtered.
Let us close this data frame moviesComDr for now.
This filter function can also be written using the match operator.
In the Source window, type the following command.
%in% is used for value matching.
To know more about this operator, let us access the Help.
In the Console window, type the following command and press Enter.
Run the last two lines of code.
moviesComDrP opens in the Source window.
The movies having genre as either Comedy or Drama have been filtered.
Let us close this data frame moviesComDrP for now.
In the Source window, click on movies.
In the Source window, scroll from left to right.
Let us now filter movies with genre as Comedy and imdb underscore rating greater than or equal to 7 point 5.
Click on the script myVis.R
In the Source window, type the following command.
Here, we have used a logical AND operator to include both conditions.
Save the script and run the last two lines of code.
moviesComIm opens in the Source window.
I will resize the Console window.
There are seven movies with genre as Comedy and imdb underscore rating greater than or equal to 7 point 5.
Let us close this data frame moviesComIm for now.
In the Source window, click on movies.
Suppose, we want to arrange the movies in an ascending order of imdb underscore rating.
For this, we will use the arrange function.
Click on the script myVis.R
In the Source window, type the following command.
Run the last two lines of code.
moviesImA opens in the Source window.
In the Source window, scroll from left to right and locate the imdb underscore rating column.
The movies have been arranged in ascending order of imdb rating.
Now, let us say we want to arrange the movies in descending order of imdb rating.
For this, we use desc function.
Let us close this data frame moviesImA for now.
In the Source window, type the following command.
Run the last two lines of code.
moviesImD opens in the Source window.
In the Source window, scroll from left to right and locate the imdb underscore rating column.
The movies have been arranged in descending order of imdb rating.
Let us close this data frame moviesImD for now.
In the Source window, click on movies.
Suppose we want to arrange the movies both by genre and imdb rating.
Click on the script myVis.R
In the Source window, type the following commands.
Run the last two lines of code.
moviesGeIm opens in the Source window.
In the Source window, scroll from left to right.
Movies have been arranged both by genre and imdb rating.
Let us summarize what we have learnt.
In this tutorial, we have learnt about:
Data manipulation
dplyr package
How to use filter and arrange functions.
We now suggest an assignment.
Consider the built-in data set mtcars. Find the cars with hp greater than 100 and cyl equal to 3.
Arrange the mtcars data set based on mpg variable.
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 , 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.