Welcome to this tutorial on More functions in the dplyr package.
In this tutorial,we will learn about the following functions in the dplyr package:
select
rename
mutate
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
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 the DataVis folder as my Working Directory.
Let us switch to RStudio.
Open the script myVis.R in RStudio.
We have already learnt how to use the filter and arrange functions in the dplyr package.
Run this script by clicking on the Source button.
movies data frame and other filtered data frames open in the Source window.
We will close all the data frames except movies.
In the Source window, scroll from left to right.
This will enable us to see the remaining objects of the movies data frame.
To select the required variables of a data frame we will use the select function.
It helps us to select only those variables that are required.
Here, we will use the select function to select title, genre, and imdb rating for all the movies.
Click on the script myVis.R
In the Source window, type the following command.
The first argument in the select function is the name of the data frame. Here it is movies.
Other arguments are the variables which we will select for all the movies.
Save the script and run the last two lines of code by pressing Ctrl + Enter keys simultaneously.
moviesTGI opens in the Source window.
Here, title, genre, and imdb rating of all the movies are displayed.
Let us close moviesTGI data frame for now.
In the Source window, click on movies data frame.
Scroll the data frame from right to left to see other columns.
In the data frame, we can see the variables like thtr_rel_day, thtr_rel_month, thtr_rel_year.
These variables provide information about the day, month and year of the theater release of the movies.
Let us select these three variables along with the title of all the movies.
Please note that all the theater-related variable names start with t h t r.
Click on the script myVis.R.
In the Source window, type the following command.
Here, we have used starts_with function.
It selects all the variables in the movies data frame, whose names start with t h t r.
Run the last two lines of code.
moviesTHT opens in the Source window.
Movies with their titles and theater-release information are shown.
Let us close moviesTHT data frame for now.
In the Source window, click on movies.
Let us change the name of the variable thtr_rel_year.
For that, we will use the rename function.
Click on the script myVis.R
In the Source window, type the following command.
Here, we are changing the name of the variable thtr_rel_year.
Run the last two lines of code.
moviesR opens in the Source window.
In the Source window, scroll from left to right.
Observe that the name of the variable thtr_rel_year has changed to rel_year.
Let us close the data frame moviesR for now.
In the Source window, click on movies.
In the Source window, scroll from left to right.
Suppose we want to add a new variable named CriAud to our movies data frame.
This variable should contain the difference between critics_score and audience_score. For this, we will use the mutate function.
mutate function is used to add a new variable and preserve the existing one.
For simplicity let us remove the variables appearing after audience_score in the movies data frame.
In the Source window, scroll from right to left.
We need to select the variables from title to audience_score.
For this, we will use the select function.
Click on the script myVis.R
In the Source window, type the following command.
Run the current line.
Now, we will use the mutate function to add a new variable.
In the Source window, type the following command.
Remember, we are adding a new variable named CriAud in the movies data frame.
This is to store the difference of critics score and audience score.
Run the last two lines of code.
moviesMu opens in the Source window.
In the Source window, scroll from left to right.
A new variable named CriAud is added.
Let us summarize what we have learnt.
In this tutorial, we have learnt about the following functions available in the dplyr package:
select
rename
mutate
We now suggest an assignment.
Use the built-in data set airquality. Using select function select the variables Ozone, Wind, and Temp in this data set.
Use the built-in data set mtcars. Rename the variables mpg and cyl with MilesPerGallon and Cylinder, respectively.
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.