R/C2/Lists-and-its-Operations/English

From Script | Spoken-Tutorial
Revision as of 17:08, 22 April 2019 by Sudhakarst (Talk | contribs)

Jump to: navigation, search

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]

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