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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 6: Line 6:
  
 
{| border =1
 
{| border =1
|'''Visual Cue'''
+
|'''Visual Cue’’’
|'''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'''
Line 37: Line 32:
  
 
|| 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 61:
  
 
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 68:
 
|-
 
|-
 
||  
 
||  
|| 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'''.  
 
|-
 
|-
Line 110: Line 104:
 
|-
 
|-
 
|| 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]
Line 125: Line 119:
 
|-
 
|-
 
|| 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 128:
 
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.  
Line 147: Line 140:
  
 
List in R
 
List in R
|| * A vector having all elements of the same type is called '''atomic vector''', whereas
+
||  
 +
* 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 elements of different type 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'''.  
 
|-
 
|-
 
||  
 
||  
Line 165: Line 159:
  
 
'''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.  
Line 175: Line 169:
  
 
'''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 185: Line 179:
 
* '''matrix''' to '''matrixA'''
 
* '''matrix''' to '''matrixA'''
 
* '''vector''' to '''myVector'''
 
* '''vector''' to '''myVector'''
 +
  
 
|-
 
|-
Line 191: Line 186:
  
 
'''print(myList)'''
 
'''print(myList)'''
| In the '''Source''' window, now type '''print, myList '''in parentheses.  
+
|| In the '''Source''' window, now type '''print, myList '''in parentheses.  
 
|-
 
|-
 
||  
 
||  
Line 197: Line 192:
 
Highlight the output in the '''Console''' window
 
Highlight the output in 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.
 
In the '''Console''' window, three elements of '''myList''' are shown with their respective names.
 +
  
 
Hence, '''myList''' is a named list.  
 
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'''.  
 
|-
 
|-
 
||  
 
||  
Line 216: Line 214:
 
|-
 
|-
 
|| 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'''.
 
|-
 
|-
 
||  
 
||  
Line 233: Line 231:
 
|-
 
|-
 
|| 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'''.  
Line 240: Line 238:
  
 
'''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 255: Line 253:
 
[RStudio]
 
[RStudio]
  
'''myList[[2]][,3]'''
+
 
 +
'''<nowiki>myList[[2]][,3]</nowiki>'''
 
|| For this, we use double square brackets.  
 
|| For this, we use double square brackets.  
  
Line 263: Line 262:
 
|| [RStudio]
 
|| [RStudio]
  
Highlight [[2]] in the '''Source''' window  
+
<nowiki>Highlight [[2]] in the </nowiki>'''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]
Line 271: Line 270:
 
|| 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.  
 
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.
+
 
 +
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 286:
 
|-
 
|-
 
|| 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]
Line 288: Line 295:
  
 
'''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.  
Line 312: Line 319:
  
 
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 374: Line 381:
  
 
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.

Revision as of 17:20, 22 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

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

Save the script and run the last three lines of code by pressing Ctrl + Enter keys simultaneously.

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 type 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.
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

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

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]


myList[[2]][,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