Difference between revisions of "R/C2/Lists-and-its-Operations/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(7 intermediate revisions by 2 users not shown)
Line 9: Line 9:
 
|'''Narration'''
 
|'''Narration'''
 
|-
 
|-
 
 
|| Show slide  
 
|| Show slide  
  
 
Opening slide
 
Opening slide
 
+
|| Welcome to the spoken tutorial on '''Lists and its Operations'''.  
|| Welcome to the spoken tutorial on''' Lists and its Operations'''.  
+
 
+
 
|-
 
|-
 
 
|| Show slide  
 
|| Show slide  
  
Line 23: Line 19:
  
 
|| In this tutorial, we will learn how to:  
 
|| In this tutorial, we will learn how to:  
 
+
* Create a '''list''' in '''R'''
* Create a '''list''' in R
+
 
* Access the elements of a '''list'''
 
* Access the elements of a '''list'''
 
* Perform indexing on '''lists'''
 
* Perform indexing on '''lists'''
Line 34: Line 29:
 
Pre-requisites
 
Pre-requisites
  
spoken-tutorial.org
+
https://spoken-tutorial.org
  
 
|| To understand this tutorial, you should know,  
 
|| To understand this tutorial, you should know,  
 
 
* '''Data frames''' in''' R'''  
 
* '''Data frames''' in''' R'''  
 
* '''Matrices''' and '''vectors''' in''' R'''
 
* '''Matrices''' and '''vectors''' in''' R'''
 +
 
If not, please locate the relevant tutorials on '''R''' on this website.
 
If not, please locate the relevant tutorials on '''R''' on this website.
 
|-
 
|-
Line 45: Line 40:
  
 
System Specifications
 
System Specifications
 
 
|| This tutorial is recorded on
 
|| This tutorial is recorded on
* '''Ubuntu Linux '''OS version''' 16.04'''
+
* '''Ubuntu Linux '''OS version 16.04  
* '''R '''version''' 3.4.4'''
+
* '''R '''version 3.4.4  
* '''RStudio''' version''' 1.1.456'''
+
* '''RStudio''' version 1.1.456  
 +
 
 
Install '''R''' version '''3.2.0''' or higher.  
 
Install '''R''' version '''3.2.0''' or higher.  
 
|-
 
|-
Line 55: Line 50:
  
 
Download files  
 
Download files  
 +
|| For this tutorial, we will use,
  
|| For this tutorial, we will use
 
 
* A '''data frame CaptaincyData.csv''' and  
 
* A '''data frame CaptaincyData.csv''' and  
 
* A '''script '''file '''myList.R'''.  
 
* A '''script '''file '''myList.R'''.  
Line 65: Line 60:
  
 
Highlight '''CaptaincyData.csv''' and''' myList.R '''in the folder '''Lists'''
 
Highlight '''CaptaincyData.csv''' and''' myList.R '''in the folder '''Lists'''
 
 
|| I have downloaded and moved these files to '''Lists''' folder.  
 
|| I have downloaded and moved these files to '''Lists''' folder.  
  
Line 73: Line 67:
 
|-
 
|-
 
||  
 
||  
|| Let us switch to '''RStudio'''.
+
|| Let us switch to '''RStudio'''.
 
|-
 
|-
|| Highlight '''myList.R''' in the '''Files '''window''' '''of '''RStudio '''
+
|| Highlight '''myList.R''' in the '''Files '''window of '''RStudio '''
 
+
 
|| Open the '''script myList.R '''in''' RStudio'''.  
 
|| Open the '''script myList.R '''in''' RStudio'''.  
 
|-
 
|-
||  
+
|| Drag the boundary of the window.
 
|| I am resizing the '''Source''' window
 
|| I am resizing the '''Source''' window
 
|-
 
|-
Line 89: Line 82:
 
|-
 
|-
 
|| Highlight '''captaincy''' in the '''Source''' window
 
|| Highlight '''captaincy''' in the '''Source''' window
|| Now let us create a matrix from a '''subset''' of '''captaincy. '''
+
|| Now let us create a matrix from a subset of '''captaincy. '''
 
|-
 
|-
 
|| Highlight '''captaincy''' in the '''Source''' window
 
|| Highlight '''captaincy''' in the '''Source''' window
|| We will convert the values in first three rows of '''played''', '''won''' and '''lost''' columns into a '''matrix'''.  
+
|| We will convert the values in first three rows of '''played, won''' and '''lost''' columns into a '''matrix'''.  
 
|-
 
|-
 
|| Highlight '''myList.R '''in the '''Source''' window  
 
|| Highlight '''myList.R '''in the '''Source''' window  
|| Click on the '''script''' '''myList.R'''
+
|| Click on the '''script myList.R'''
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
Line 103: Line 96:
 
'''matrixA <- as.matrix(subData)'''
 
'''matrixA <- as.matrix(subData)'''
  
|| In the '''Source '''window, type the following commands.  
+
|| In the '''Source '''window, type the following '''commands'''.  
  
 
Press '''Enter'''.  
 
Press '''Enter'''.  
  
Press '''Enter''' at the end of every command.  
+
Press '''Enter''' at the end of every '''command'''.  
 
|-
 
|-
 
|| Highlight '''as.matrix''' in the '''Source '''window
 
|| Highlight '''as.matrix''' in the '''Source '''window
| Remember, '''as.matrix''' function is used to convert '''subData '''into a '''matrix'''.  
+
|| Remember, '''as.matrix function''' is used to convert '''subData '''into a '''matrix'''.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
  
 
'''myVector <- c(1:5)'''
 
'''myVector <- c(1:5)'''
|| Now let us create a numeric vector '''myVector.'''
+
|| Now let us create a numeric '''vector myVector.'''
  
In the '''Source''' window, type the following command.  
+
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.
 
 
|-
 
|-
||  
+
|| Press Ctrl + S >> Ctrl + Enter
 +
||Save the '''script''' and run the last three lines of code by pressing '''Ctrl + Enter''' keys simultaneously.
 +
|-
 +
|| Drag the boundary of the window
 
|| I am resizing the '''Source''' window
 
|| I am resizing the '''Source''' window
 
|-
 
|-
 
|| Highlight the objects in '''Environment''' window  
 
|| Highlight the objects in '''Environment''' window  
| The three objects '''subData''', '''matrixA''' and '''myVector''' are loaded in our '''workspace'''.  
+
|| 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.  
+
|| We will learn how to create a '''list''' in '''R''' by using the '''list function'''.  
 
|-
 
|-
 
|| Show Slide
 
|| Show Slide
Line 134: Line 129:
 
List in R
 
List in R
  
| A '''list''' is an R object.
+
|| A '''list''' is an '''R object'''.
  
It has components of mixed data types like  
+
It has components of mixed '''data types''' like  
* strings
+
* '''strings'''
* numbers
+
* '''numbers'''
* vectors
+
* '''vectors'''
* some other list inside it.
+
* some other '''list''' inside it.  
  
A list can also contain a matrix or a function as its elements.  
+
A '''list''' can also contain a '''matrix''' or a '''function''' as its '''elements'''.  
 
|-
 
|-
 
|| Show Slide
 
|| Show Slide
  
 
List in R
 
List in R
|| * A vector having all elements of the same type is called '''atomic vector''', whereas
+
||  
* A vector having elements of different type is called '''list'''
+
* 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'''.
+
|| Let us switch to '''RStudio'''.
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
  
Highlight '''captaincy''', '''matrixA''' and '''myVector''' in the '''Environment''' window  
+
Highlight '''captaincy, matrixA''' and '''myVector''' in the '''Environment''' window  
| We will create a '''list''' containing '''captaincy''', '''matrixA''' and '''myVector'''.  
+
|| We will create a '''list''' containing '''captaincy, matrixA''' and '''myVector'''.  
 
|-
 
|-
||  
+
|| Drag the boundary of the window.
 
|| I am resizing the '''Source''' window
 
|| I am resizing the '''Source''' window
 
|-
 
|-
Line 165: Line 161:
  
 
'''myList <- list(captaincy, matrixA, myVector)'''
 
'''myList <- list(captaincy, matrixA, myVector)'''
| In the '''Source''' window, type the following command.  
+
|| In the '''Source''' window, type the following '''command'''.  
 
|-
 
|-
 
|| Highlight '''myList''' in the '''Source''' window  
 
|| Highlight '''myList''' in the '''Source''' window  
| Now, we will assign names to the elements of '''myList'''.  
+
|| Now, we will assign names to the '''elements''' of '''myList'''.  
  
For this, we will use '''names''' function.  
+
For this, we will use '''names function'''.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
  
 
'''names(myList) <- c("dataframe", "matrix", "vector")'''
 
'''names(myList) <- c("dataframe", "matrix", "vector")'''
| In the '''Source''' window, type the following command.  
+
|| In the '''Source''' window, type the following '''command'''.  
 
|-
 
|-
 
|| Highlight '''dataframe''' in the '''Source''' window
 
|| Highlight '''dataframe''' in the '''Source''' window
Line 182: Line 178:
  
 
Highlight '''vector''' in the '''Source''' window  
 
Highlight '''vector''' in the '''Source''' window  
|| We will assign the following names,* '''dataframe''' to '''captaincy'''.
+
|| We will assign the following names,
 +
* '''dataframe''' to '''captaincy'''.
 
* '''matrix''' to '''matrixA'''
 
* '''matrix''' to '''matrixA'''
 
* '''vector''' to '''myVector'''
 
* '''vector''' to '''myVector'''
Line 188: Line 185:
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
 
  
 
'''print(myList)'''
 
'''print(myList)'''
| In the '''Source''' window, now type '''print, myList '''in parentheses.  
+
|| In the '''Source''' window, now type '''print, myList '''in parentheses.  
 
|-
 
|-
||  
+
|| Highlight the output in the '''Console''' window.
  
Highlight the output in the '''Console''' window
+
Drag the boundary of the console window.
 
|| Run the last three lines of code.  
 
|| Run the last three lines of code.  
  
 
I am resizing the '''Console''' window.  
 
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.  
+
In the '''Console''' window, three '''elements''' of '''myList''' are shown with their respective names.
 +
 
 +
 
 +
Hence, '''myList''' is a named '''list'''.  
 
|-
 
|-
 
|| Highlight '''myList''' in the '''Source''' window  
 
|| Highlight '''myList''' in the '''Source''' window  
| Now, we will learn how to access the elements of a '''list'''.  
+
|| Now, we will learn how to access the '''elements''' of a '''list'''.  
 
|-
 
|-
||  
+
|| Drag the boundary of the console window.
 
|| I am resizing the '''Console''' window.  
 
|| I am resizing the '''Console''' window.  
 
|-
 
|-
||  
+
|| Cursor on the interface.
|| Elements of the list can be accessed by the index of the elements in the list. It is also known as indexing.  
+
|| '''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.  
+
In case of named '''lists''', it can also be accessed by using the names.  
 
|-
 
|-
 
|| Highlight '''dataframe''' in the '''Console''' window  
 
|| Highlight '''dataframe''' in the '''Console''' window  
| Suppose we want to access the element named as '''dataframe''' of '''myList'''.  
+
|| Suppose we want to access the '''element''' named as '''dataframe''' of '''myList'''.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
  
 
'''myList$dataframe'''
 
'''myList$dataframe'''
| In the '''Source''' window, type''' myList dollar sign dataframe'''
+
|| In the '''Source''' window, type''' myList dollar sign dataframe'''
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
Line 227: Line 225:
 
|-
 
|-
 
|| Highlight the output in the '''Console''' window
 
|| Highlight the output in the '''Console''' window
| '''captaincy dataframe''', which is an element of '''myList''', is shown on the '''Console'''.
+
|| '''captaincy dataframe''', which is an '''element''' of '''myList''', is shown on the '''Console'''.
 
|-
 
|-
 
||  
 
||  
|| We can also access an element by using its index.
+
|| We can also access an '''element''' by using its '''index'''.
 
|-
 
|-
 
|| Highlight '''matrix''' in the '''Source''' window
 
|| Highlight '''matrix''' in the '''Source''' window
| Suppose we want to access the element named matrix of '''myList'''.  
+
|| Suppose we want to access the '''element''' named '''matrix''' of '''myList'''.  
  
Index of '''matrix''' is 2 in '''myList'''.  
+
'''Index''' of '''matrix''' is 2 in '''myList'''.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
  
 
'''myList[2]'''
 
'''myList[2]'''
| In the '''Source''' window, type '''myList''' and 2 in square brackets.  
+
|| In the '''Source''' window, type '''myList''' and 2 in square brackets.  
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
Line 246: Line 244:
 
|-
 
|-
 
|| Highlight the output in the '''Console''' window
 
|| Highlight the output in the '''Console''' window
| '''matrix '''element of '''myList''' is shown on the '''Console'''.
+
|| '''matrix element''' of '''myList''' is shown on the '''Console'''.
 
|-
 
|-
 
|| Highlight '''matrix''' in the '''Console''' window
 
|| Highlight '''matrix''' in the '''Console''' window
| Now, suppose we want to access the third column of '''matrix''' element of '''myList'''.  
+
|| Now, suppose we want to access the third column of '''matrix element''' of '''myList'''.  
 
|-
 
|-
 
||  
 
||  
Line 258: Line 256:
 
|| For this, we use double square brackets.  
 
|| For this, we use double square brackets.  
  
 
+
In the '''Source''' window, type the following '''command'''.  
In the '''Source''' window, type the following command.  
+
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
  
 
Highlight [[2]] in the '''Source''' window  
 
Highlight [[2]] in the '''Source''' window  
| 2 in double square brackets denotes that second element of '''myList''' is to be accessed.  
+
|| 2 in double square brackets denotes that second '''element''' of '''myList''' is to be accessed.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
  
 
Highlight [,3] in the '''Source''' window  
 
Highlight [,3] in the '''Source''' window  
|| comma 3 in single square brackets denotes that third column of second element 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.  
+
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.
+
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.
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
Line 279: Line 278:
 
|-
 
|-
 
|| Highlight the output in the '''Console''' window
 
|| Highlight the output in the '''Console''' window
| The elements of third column of matrix element are displayed on the '''Console'''.  
+
|| The elements of third column of '''matrix element''' are displayed on the '''Console'''.  
 
|-
 
|-
 
||  
 
||  
| Now, let us learn how to combine two different lists.  
+
|| Now, let us learn how to combine two different '''lists'''.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
 
  
 
'''listSimple <- list("One", "Two", "Three")'''
 
'''listSimple <- list("One", "Two", "Three")'''
| First, we will create a simple list.
+
|| First, we will create a simple '''list'''.
  
In the '''Source''' window, type the following command.  
+
In the '''Source''' window, type the following '''command'''.  
  
 
Save the '''script''' and run the current line.  
 
Save the '''script''' and run the current line.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
 
  
 
'''merged.list <- c(myList, listSimple)'''
 
'''merged.list <- c(myList, listSimple)'''
Line 301: Line 298:
 
'''print(merged.list) '''
 
'''print(merged.list) '''
  
 
+
|| Now, we declare an '''object''' named '''merged dot list.'''
 
+
|| Now, we declare an object named '''merged dot list.'''
+
  
 
It is used for storing the combined output of '''myList''' and '''listSimple'''.
 
It is used for storing the combined output of '''myList''' and '''listSimple'''.
  
In the '''Source''' window, type the following commands.  
+
In the '''Source''' window, type the following '''commands'''.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
  
 
Highlight the output in the '''Console''' window
 
Highlight the output in the '''Console''' window
| Save the '''script''' and run the last three lines of code.  
+
|| Save the '''script''' and run the last three lines of code.  
  
 
I am maximizing the '''Console''' window.  
 
I am maximizing the '''Console''' window.  
Line 318: Line 313:
 
Now, scroll up to locate the output.  
 
Now, scroll up to locate the output.  
  
The combined list is shown on the '''Console'''.  
+
The combined '''list''' is shown on the '''Console'''.  
 
|-
 
|-
 
||  
 
||  
Line 329: Line 324:
 
|| In this tutorial, we have learned how to
 
|| In this tutorial, we have learned how to
 
* Create a '''list''' in R
 
* Create a '''list''' in R
* Access the elements of a '''list'''
+
* Access the '''elements''' of a '''list'''
* Perform indexing on '''lists'''
+
* Perform '''indexing''' on '''lists'''
 
* Combine two '''lists'''
 
* Combine two '''lists'''
 
  
 
|-
 
|-
Line 339: Line 333:
 
Assignment
 
Assignment
 
|| We now suggest an assignment.
 
|| 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 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.  
+
* 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.  
+
* 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'''.  
+
* 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.  
 
For solutions, please refer to the '''Additional Material '''section on this website.  
Line 357: Line 351:
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
 
|| We conduct workshops using Spoken Tutorials and give certificates.
 
|| We conduct workshops using Spoken Tutorials and give certificates.
 
  
 
Please contact us.
 
Please contact us.
Line 374: Line 367:
  
 
Textbook Companion
 
Textbook Companion
| The '''FOSSEE '''team coordinates the '''TBC '''project.
+
|| The '''FOSSEE '''team coordinates the '''TBC '''project.
  
 
For more details, please visit these sites.
 
For more details, please visit these sites.
Line 387: Line 380:
 
Thank You
 
Thank You
 
|| The script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018).
 
|| 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.
 
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.
 
|-
 
|-
 
|}
 
|}

Latest revision as of 22:25, 26 April 2019

Title of script: Lists and its Operations

Author: Shaik Sameer (IIIT Vadodara) and Sudhakar Kumar (IIT Bombay)

Keywords: R, RStudio, lists, indexing, slicing, video tutorial

Visual Cue Narration
Show slide

Opening slide

Welcome to the spoken tutorial on Lists and its Operations.
Show slide

Learning Objectives

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
Show slide

Pre-requisites

https://spoken-tutorial.org

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.

Show slide

System Specifications

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.

Show slide

Download files

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.

[Computer screen]

Highlight CaptaincyData.csv and myList.R in the folder Lists

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.
Highlight myList.R in the Files window of RStudio Open the script myList.R in RStudio.
Drag the boundary of the window. I am resizing the Source window
Highlight the Source button Run this script by clicking on Source button.
Highlight captaincy in the Source window captaincy data frame opens in the Source window.
Highlight captaincy in the Source window Now let us create a matrix from a subset of captaincy.
Highlight captaincy in the Source window We will convert the values in first three rows of played, won and lost columns into a matrix.
Highlight myList.R in the Source window Click on the script myList.R
[RStudio]

subData <- captaincy[1:3, c("played", "won", "lost")]

matrixA <- as.matrix(subData)

In the Source window, type the following commands.

Press Enter.

Press Enter at the end of every command.

Highlight as.matrix in the Source window Remember, as.matrix function is used to convert subData into a matrix.
[RStudio]

myVector <- c(1:5)

Now let us create a numeric vector myVector.

In the Source window, type the following command.

Press Ctrl + S >> Ctrl + Enter Save the script and run the last three lines of code by pressing Ctrl + Enter keys simultaneously.
Drag the boundary of the window I am resizing the Source window
Highlight the objects in Environment 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.
Show Slide

List in R

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.

Show Slide

List in R

  • 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.
[RStudio]

Highlight captaincy, matrixA and myVector in the Environment window

We will create a list containing captaincy, matrixA and myVector.
Drag the boundary of the window. I am resizing the Source window
[RStudio]

myList <- list(captaincy, matrixA, myVector)

In the Source window, type the following command.
Highlight myList in the Source window Now, we will assign names to the elements of myList.

For this, we will use names function.

[RStudio]

names(myList) <- c("dataframe", "matrix", "vector")

In the Source window, type the following command.
Highlight dataframe in the Source window

Highlight matrix in the Source window

Highlight vector in the Source window

We will assign the following names,
  • dataframe to captaincy.
  • matrix to matrixA
  • vector to myVector
[RStudio]

print(myList)

In the Source window, now type print, myList in parentheses.
Highlight the output in the Console window.

Drag the boundary of the console window.

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.

Highlight myList in the Source window Now, we will learn how to access the elements of a list.
Drag the boundary of the console window. I am resizing the Console window.
Cursor on the interface. 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.

Highlight dataframe in the Console window Suppose we want to access the element named as dataframe of myList.
[RStudio]

myList$dataframe

In the Source window, type myList dollar sign dataframe
Highlight Run button in the Source window Run the current line.
Highlight the output in the Console window captaincy dataframe, which is an element of myList, is shown on the Console.
We can also access an element by using its index.
Highlight matrix in the Source window Suppose we want to access the element named matrix of myList.

Index of matrix is 2 in myList.

[RStudio]

myList[2]

In the Source window, type myList and 2 in square brackets.
Highlight Run button in the Source window Run the current line.
Highlight the output in the Console window matrix element of myList is shown on the Console.
Highlight matrix in the Console window Now, suppose we want to access the third column of matrix element of myList.

[RStudio]

myList2[,3]

For this, we use double square brackets.

In the Source window, type the following command.

[RStudio]

Highlight 2 in the Source window

2 in double square brackets denotes that second element of myList is to be accessed.
[RStudio]

Highlight [,3] in the Source window

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.

Highlight Run button in the Source window Run the current line.
Highlight the output in the Console window The elements of third column of matrix element are displayed on the Console.
Now, let us learn how to combine two different lists.
[RStudio]

listSimple <- list("One", "Two", "Three")

First, we will create a simple list.

In the Source window, type the following command.

Save the script and run the current line.

[RStudio]

merged.list <- c(myList, listSimple)

print(merged.list)

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.

[RStudio]

Highlight the output in the Console window

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.
Show Slide

Summary

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
Show Slide

Assignment

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.

Show slide

About the Spoken Tutorial Project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Show slide

Spoken Tutorial Workshops

We conduct workshops using Spoken Tutorials and give certificates.

Please contact us.

Show Slide

Forum to answer questions

Please post your timed queries in this forum.
Show Slide

Forum to answer questions

Please post your general queries in this forum.
Show Slide

Textbook Companion

The FOSSEE team coordinates the TBC project.

For more details, please visit these sites.

Show Slide

Acknowledgement

The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
Show Slide

Thank You

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.

Contributors and Content Editors

Madhurig, Nancyvarkey, Sudhakarst