Welcome to the spoken tutorial on Merging and Importing Data
In this tutorial, we will learn how to:
Use built-in functions for exploring a data frame
Merge two data frames.
Import data in different formats in R
To understand this tutorial, you should know
Data frames in R
R script in RStudio
How to set working directory in RStudio
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.456
Install R version 3.2.0 or higher.
For this tutorial, we will use,
five data frames in different formats and
a script file myDataSet.R.
Please download these files from the Code files link of this tutorial.
I have downloaded these files from Code files link and moved them to DataMerging folder in myProject folder on the Desktop.
I have also set this folder as my Working Directory.
Let us switch to RStudio.
Open the script myDataSet.R in RStudio.
For this, click on the script myDataSet.R.
Script myDataSet.R opens in Rstudio.
Run this script by clicking on Source button.
captaincyOne appears in the Source window.
We will use some built-in functions of R to explore captaincyOne.
For all the built-in functions used in this tutorial, please refer to the Additional Material.
First, we will use summary function.
Click on the script myDataSet.R
In the Source window, type summary and then captaincyOne in parentheses.
Save the script and run the current line by pressing Ctrl+Enter keys simultaneously.
In the Console window, scroll up to locate the output.
Statistical parameters for each column of captaincyOne are shown on the Console.
In the Source window, press Enter.
Press Enter at the end of every command.
Now, let us look at class function.
In the Source window, type class and then captaincyOne in parentheses.
Save the script and run the current line.
class function returns the class of captaincyOne, which is data frame.
Next let us look at typeof function.
In the Source window, type typeof and then captaincyOne in parentheses.
Save the script and run the current line.
typeof function returns the storage type of captaincyOne, which is list.
To know more about typeof function, we will access the help section of RStudio.
In the Console window, type help, within parentheses typeof. Press Enter.
typeof determines the R internal type or storage mode of any object.
Click on the Files tab.
Clear the Console window by clicking on the broom icon.
Click on the data frame captaincyOne.
Now, let us extract two rows from top of captaincyOne.
For this, we will use head function.
Click on the script myDataSet.R
In the Source window, type head within parentheses captaincyOne comma space 2.
Save the script and run the current line.
The top two rows of captaincyOne are shown on the Console window.
Click on the data frame captaincyOne.
Suppose we want to extract two rows from bottom of captaincyOne.
For this, we will use the tail function.
Click on the script myDataSet.R
In the Source window, type tail within parentheses captaincyOne comma space 2.
Save the script and run the current line.
The last two rows of captaincyOne are shown on the Console window.
Next, let us learn about str function.
This function is used to display the structure of an R object.
In the Source window, type str within parentheses captaincyOne.
Save the script and run the current line.
The structural details of captaincyOne are shown on the Console.
Now, we will look at merging of data frames.
Merging data frames has advantages like:
It makes data more available.
It helps in improving data quality.
Combining similar data also reduces data complexity.
Let us switch to RStudio.
We will learn how to merge two data frames CaptaincyData.csv and CaptaincyData2.csv.
We will declare a variable captaincyTwo to store and read CaptaincyData2.csv.
In the Source window, type the following command and press Enter.
Now, type View within parentheses captaincyTwo.
Save the script and run the last two lines.
The contents of captaincyTwo appear in the Source window.
This data frame has the same captains as that in captaincyOne.
However, it has different information about them like the number of matches drawn.
Now, we will update captaincyOne by adding information from captaincyTwo.
For this, we use merge function.
Click on the script myDataSet.R
I am resizing the Source window.
In the Source window, type the following command.
Press Enter.
In the merge function, we use column names by which we want to merge two data frames.
Here, it is names.
Now, type View and captaincyOne in parentheses.
Save the script and run these two lines.
The contents of the updated captaincyOne appear in the Source window.
Close the two tabs captaincyOne and captaincyTwo.
Now, we will learn how to import data of different formats in R.
We shall add one comment first.
In the Source window, type # hash space Importing data in different formats.
Now, let us import CaptaincyData.xml file.
For that, we need to install XML package.
Make sure that you are connected to Internet.
We need to install Ubuntu package libxml2-dev before installing XML package.
Information on how to install this package, is provided in the Additional Material.
I have already installed libxml2-dev package.
Hence, I will proceed for installing XML package now.
On the Console window, type install dot packages.
Now, type XMLinside double quotes and in parentheses.
Press Enter. We will wait until R installs the package.
Then, we load this package using library function.
Click on the script myDataSet.R
Since we are loading a package, we will add it at the top of the script.
In the Source window, scroll up.
Now, at the top of the script myDataSet.R, type library and XML in parentheses.
Save the script and run this line.
Now, in the Source window, click on the next line after the comment Importing data in different formats.
Type the following command and press Enter.
Then type View and xmldata in parentheses.
Save the script and run these two lines.
The contents of the xml file are shown here.
Next let us learn how to import CaptaincyData.txt.
Click on the script myDataSet.R
In the Source window, type the following command
and press Enter.
Next, type View and txtdata in parentheses.
Save the script and run these two lines.
The contents of the txt file are shown.
Now, we will learn how to import data from user interface of Rstudio.
I am resizing the Source window.
We will import the Excel file CaptaincyData.xlsx using this method.
Please ensure that you have packages like readxl and Rcpp installed in your system.
In the top right corner of RStudio, click on the Environment tab.
In the Environment tab, click on Import Dataset.
From the drop-down menu, select From Excel.
A window named Import Excel Data appears.
You can select a file on your computer or type the URL from which you want to load an Excel file.
We will select a file on our computer.
In the upper right corner of this window, near File/Url text field, click on Browse.
I will select the file CaptaincyData.xlsx located in DataMerging folder.
This folder is in myProject folder on the Desktop.
Click Open to load this file.
Below the field File/Url, RStudio shows the preview of the Excel file being imported.
At the bottom right corner of this window, you can see the code for importing this Excel file.
Finally, click on the Import button.
The contents of the Excel file are shown here.
Let us summarize what we have learnt.
In this tutorial, we have learnt how to:
Use built-in functions for exploring a data frame
Merge two data frames
Import data in different formats in R
We now suggest an assignment.
Using built-in dataset iris, implement all the functions we have learnt in this tutorial.
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 Shaik Sameer (FOSSEE Fellow 2018).
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.