Welcome to the spoken tutorial on Lists and its Operations.
In this tutorial, we will learn how to:
Create a list in R
Access the elements of a list
Perform indexing on lists
Combine two lists
To understand this tutorial, you should know,
Data frames in R
Matrices and vectors in R
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,
A data frame CaptaincyData.csv
and A script file myList.R.
Please download these files from the Code files link of this tutorial.
I have downloaded and moved these files to Lists folder.
This folder is located in myProject folder on my Desktop.
I have also set this folder as my Working Directory.
Let us switch to RStudio.
Open the script myList.R in RStudio.
I am resizing the Source window
Run this script by clicking on Source button.
captaincy data frame opens in the Source window.
Now let us create a matrix from a subset of captaincy.
We will convert the values in first three rows of played, won and lost columns into a matrix.
Click on the script myList.R
In the Source window, type the following commands.
Press Enter.
Press Enter at the end of every command.
Remember, as.matrix function is used to convert subData into a matrix.
Now let us create a numeric vector myVector.
In the Source window, type the following command.
Save the script and run the last three lines of code by pressing Ctrl + Enter keys simultaneously.
I am resizing the Source window
The three objects subData, matrixA and myVector are loaded in our workspace.
We will learn how to create a list in R by using the list function.
A list is an R object.
It has components of mixed data types like
strings
numbers
vectors
some other list inside it.
A list can also contain a matrix or a function as its elements.
A vector having all elements of the same type is called atomic vector,
whereas, A vector having elements of different types is called list.
Let us switch to RStudio.
We will create a list containing captaincy, matrixA and myVector.
I am resizing the Source window
In the Source window, type the following command.
Now, we will assign names to the elements of myList.
For this, we will use names function.
In the Source window, type the following command.
We will assign the following names,
dataframe to captaincy.
matrix to matrixA
vector to myVector
In the Source window, now type print, myList in parentheses.
Run the last three lines of code.
I am resizing the Console window.
In the Console window, three elements of myList are shown with their respective names.
Hence, myList is a named list.
Now, we will learn how to access the elements of a list.
I am resizing the Console window.
Elements of the list can be accessed by the index of the elements in the list.
It is also known as indexing.
In case of named lists, it can also be accessed by using the names.
Suppose we want to access the element named as dataframe of myList.
In the Source window, type myList dollar sign dataframe
Run the current line.
captaincy dataframe, which is an element of myList, is shown on the Console.
We can also access an element by using its index.
Suppose we want to access the element named matrix of myList.
Index of matrix is 2 in myList.
In the Source window, type myList and 2 in square brackets.
Run the current line.
matrix element of myList is shown on the Console.
Now, suppose we want to access the third column of matrix element of myList.
For this, we use double square brackets.
In the Source window, type the following command.
2 in double square brackets denotes that second element of myList is to be accessed.
comma 3 in single square brackets denotes that third column of second element is to be accessed.
Remember we use single square bracket notation with two indices for indexing a data frame.
First index and second index refer to row and column respectively.
Also, leaving an index blank indicates that we want to keep all the elements in that dimension.
Run the current line.
The elements of third column of matrix element are displayed on the Console.
Now, let us learn how to combine two different lists.
First, we will create a simple list.
In the Source window, type the following command.
Save the script and run the current line.
Now, we declare an object named merged dot list.
It is used for storing the combined output of myList and listSimple.
In the Source window, type the following commands.
Save the script and run the last three lines of code.
I am maximizing the Console window.
Now, scroll up to locate the output.
The combined list is shown on the Console.
Let us summarize what we have learnt.
In this tutorial, we have learned how to
Create a list in R
Access the elements of a list
Perform indexing on lists
Combine two lists
We now suggest an assignment.
Create a numeric vector c(1:5) and a 5 by 3 matrix with elements from 1 to 15.
Create a named list with vector, matrix and iris data set.
Retrieve the iris data set from the list using dollar operator and indexing method.
State the differences between the results obtained by using dollar operator and indexing method of accessing iris.
For solutions, please refer to the Additional Material section on this website.
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.