Welcome to the spoken tutorial on Indexing and Slicing Data Frames.
In this tutorial, we will learn how to:
Extract rows or columns from a data frame
Create a subset from a data frame
Retrieve data using double square brackets
To understand this tutorial, you should have knowledge about
Data frames in R
R script in RStudio and
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.2.3
RStudio version 1.1.456
Install R version 3.2.0 or higher.
For this tutorial, we will use the data frame CaptaincyData.csv and a script file mydataframe.R.
Download these files from the Code files link of this tutorial.
I have downloaded and moved these files to the folder myProject on my Desktop.
I have also set this folder as my Working Directory.
Let us switch to Rstudio.
Let us open the script mydataframe.R in RStudio.
For this, click on the script mydataframe.R.
Script mydataframe.R opens in Rstudio.
Here, we have declared a variable captaincy to store and read CaptaincyData.csv.
Also, View function is being used to see the contents of the file.
Remember, you may also use equal to sign in place of less than symbol followed by hyphen.
However, we recommend less than symbol followed by hyphen.
For this, there is a shortcut in RStudio.
Suppose we want to assign a value of 2 to a variable named testvar.
In the Console window, type testvar and then press
Alt and -'(hyphen) keys simultaneously.
Then type 2 and press Enter.
Let us get back to the Source window.
Run the script mydataframe.R by clicking on the Source button.
Now let us extract the contents of the third row of the captaincy data frame.
Click on script mydataframe.R
In the Source window, type captaincy
Type capt and press Enter to make the variable captaincy autocomplete. Now within square brackets 3 followed by comma. And press Enter.
Press Enter key at the end of every command.
Remember one of the most important features of RStudio include intelligent auto-completion of function names, packages, and R objects.
We use a comma within square brackets when we wish to extract a row.
Save the script and execute the current line by pressing Ctrl+Enter keys.
The third row of the captaincy data frame is seen in the Console window.
Now, let us run the same command without a comma.
In the Source window, type captaincy then within square brackets 3.
Save the script and run this line only, as shown earlier.
The contents of the third column of the data frame, are displayed on the Console window.
So, to extract a column, we shouldn’t use a comma within the square brackets.
When we extract data using row number or column number, it is known as numeric indexing.
In the Source window, click on the captaincy data frame.
Let us now extract the contents of second and third rows of the captaincy data frame.
To retrieve more than one row, we use a numeric index vector. Click on the script mydataframe.R
In the Source window, type the following command and press Enter.
c function is being used to concatenate the second and third rows.
Save the script and execute the current line.
The second and third rows of captaincy data frame are seen in the Console window.
In the Source window, click on the captaincy data frame.
Now, we’ll find who has played 25 matches from the played column of captaincy data frame.
Extracting this type of information is known as logical indexing.
Click on the script mydataframe.R
In the Source window, type captaincy
Within square brackets captaincy dollar sign played equal to equal to 25 comma. Press Enter.
Remember, dollar sign allows you to extract elements by name.
Please note that there is no space between the two equal to signs.
Save the script and execute the current line.
The details of captain Dravid are shown in the Console window.
In the Source window, click on the captaincy data frame.
Now let us learn how to get the values of any particular attribute for all the players.
We will fetch the names of all the captains.
For this, we need to know the values in the first column.
Click on the script mydataframe.R
In the Source window, type captaincy and within square brackets 1.
Please note that I have not used a comma inside the square brackets.
Save the script and execute the current line.
The names of the captains are seen in the Console window.
Usually we use column names instead of column numbers.
To know the names of the captains, type captaincy.
Within square brackets inside double quotes names
Save the script and execute the current line.
Names of the captains are shown in the Console window.
Extracting data by column names is known as name indexing.
Clear the Console window by clicking on the broom icon.
In the Source window, click on the captaincy data frame
Now let us view the names of the captains along with the number of matches they have won.
Click on the script mydataframe.R
In the Source window, type the following command and press Enter.
Here c function is used to concatenate names and won.
Observe that names and won have been written within double quotes.
Save the script and execute this line.
The names of captains and the number of matches won are seen in the Console window.
Now let us extract a subset from captaincy data frame.
We will create a subset of captains, who have won more than 30% of their matches.
This is called slicing a data frame.
In the Source window, click on the captaincy data frame.
Please note that there is a column named victory in the captaincy data frame.
For required subset of captains, their victory should be greater than 0.3.
For this subset, we shall only show the
names
number of matches played
number of matches won
Click on the script mydataframe.R
I will resize the Source window.
In the Source window, type the following command
Now, press Enter after the comma followed by 0 point 3.
You can press Enter after a comma for better visibility.
The select parameter is used to select the required columns, names, played, and won.
In the Source window, type print within parentheses subData .
Save the script and run these two lines.
I am resizing the Console window to see the output properly.
The subData is shown in the Console window.
In the Source window, click on the captaincy data frame.
Finally, let us learn how to extract a particular entry from some column of a data frame.
We will extract the third value in the fourth column of the captaincy data frame.
Click on the script mydataframe.R.
In the Source window, type captaincy within double square brackets 4 within single square brackets 3.
Save the script and execute the current line.
The expected value 14 is seen in the Console window.
For more information on indexing and slicing data frames, please refer to the Additional materials section on this website.
Let us summarize what we have learnt.
In this tutorial, we have learnt how to:
Extract rows or columns from a data frame
Create a subset from a data frame
Retrieve data using double square brackets
We now suggest an assignment.
Create a subset from captaincy data frame with the captains who have played more than(>) 20 matches and lost less than(<) 14 matches.
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.