R/C2/Lists-and-its-Operations/English-timed
From Script | Spoken-Tutorial
Revision as of 18:07, 1 June 2020 by Sakinashaikh (Talk | contribs)
Time | Narration |
00:01 | Welcome to the spoken tutorial on Lists and its Operations. |
00:06 | In this tutorial, we will learn how to: |
00:11 | Create a list in R |
00:14 | Access the elements of a list |
00:18 | Perform indexing on lists |
00:21 | Combine two lists |
00:25 | To understand this tutorial, you should know, |
00:28 | Data frames in R' |
00:31 | Matrices and vectors in R |
00:35 | If not, please locate the relevant tutorials on R on this website. |
00:42 | This tutorial is recorded on |
00:45 | Ubuntu Linux OS version 16.04 |
00:51 | R version 3.4.4 |
00:55 | RStudio version 1.1.456 |
01:00 | Install R version 3.2.0 or higher. |
01:06 | For this tutorial, we will use, |
01:10 | A data frame CaptaincyData.csv |
01:15 | and A script file myList.R. |
01:20 | Please download these files from the Code files link of this tutorial. |
01:26 | I have downloaded and moved these files to Lists folder. |
01:32 | This folder is located in myProject folder on my Desktop. |
01:38 | I have also set this folder as my Working Directory. |
01:44 | Let us switch to RStudio. |
01:47 | Open the script myList.R in RStudio. |
01:53 | I am resizing the Source window |
01:58 | Run this script by clicking on Source button. |
02:02 | captaincy data frame opens in the Source window. |
02:07 | Now let us create a matrix from a subset of captaincy. |
02:13 | We will convert the values in first three rows of played, won and lost columns into a matrix. |
02:23 | Click on the script myList.R |
02:27 | In the Source window, type the following commands. |
02:31 | Press Enter. |
02:33 | Press Enter at the end of every command. |
02:37 | Remember, as.matrix function is used to convert subData into a matrix. |
02:45 | Now let us create a numeric vector myVector. |
02:51 | In the Source window, type the following command. |
02:54 | Save the script and run the last three lines of code by pressing Ctrl + Enter keys simultaneously.
|
03:06 | I am resizing the Source window |
03:10 | The three objects subData, matrixA and myVector are loaded in our workspace. |
03:17 | We will learn how to create a list in R by using the list function. |
03:23 | A list is an R object. |
03:27 | It has components of mixed data types like |
03:33 | strings |
03:35 | numbers |
03:37 | vectors |
03:38 | some other list inside it. |
03:41 | A list can also contain a matrix or a function as its elements. |
03:48 | A vector having all elements of the same type is called atomic vector, |
03:56 | whereas, A vector having elements of different types is called list. |
04:02 | Let us switch to RStudio. |
04:05 | We will create a list containing captaincy, matrixA and myVector. |
04:13 | I am resizing the Source window |
04:18 | In the Source window, type the following command. |
04:21 | Now, we will assign names to the elements of myList. |
04:27 | For this, we will use names function. |
04:31 | In the Source window, type the following command. |
04:35 | We will assign the following names, |
04:40 | dataframe to captaincy. |
04:44 | matrix to matrixA |
04:47 | vector to myVector |
04:49 | In the Source window, now type print, myList in parentheses. |
04:55 | Run the last three lines of code. |
04:59 | I am resizing the Console window. |
05:03 | In the Console window, three elements of myList are shown with their respective names. |
05:11 | Hence, myList is a named list. |
05:15 | Now, we will learn how to access the elements of a list. |
05:21 | I am resizing the Console window. |
05:25 | Elements of the list can be accessed by the index of the elements in the list. |
05:32 | It is also known as indexing. |
05:35 | In case of named lists, it can also be accessed by using the names. |
05:43 | Suppose we want to access the element named as dataframe of myList. |
05:49 | In the Source window, type myList dollar sign dataframe |
05:56 | Run the current line. |
05:58 | captaincy dataframe, which is an element of myList, is shown on the Console. |
06:04 | We can also access an element by using its index. |
06:11 | Suppose we want to access the element named matrix of myList. |
06:17 | Index of matrix is 2 in myList. |
06:21 | In the Source window, type myList and 2 in square brackets. |
06:28 | Run the current line. |
06:31 | matrix element of myList is shown on the Console. |
06:36 | Now, suppose we want to access the third column of matrix element of myList. |
06:42 | For this, we use double square brackets. |
06:47 | In the Source window, type the following command. |
06:51 | 2 in double square brackets denotes that second element of myList is to be accessed. |
07:00 | comma 3 in single square brackets denotes that third column of second element is to be accessed. |
07:08 | Remember we use single square bracket notation with two indices for indexing a data frame. |
07:17 | First index and second index refer to row and column respectively. |
07:23 | Also, leaving an index blank indicates that we want to keep all the elements in that dimension.
|
07:31 | Run the current line. |
07:34 | The elements of third column of matrix element are displayed on the Console. |
07:40 | Now, let us learn how to combine two different lists. |
07:45 | First, we will create a simple list. |
07:49 | In the Source window, type the following command. |
07:53 | Save the script and run the current line. |
07:57 | Now, we declare an object named merged dot list. |
08:03 | It is used for storing the combined output of myList and listSimple. |
08:11 | In the Source window, type the following commands. |
08:15 | Save the script and run the last three lines of code. |
08:20 | I am maximizing the Console window. |
08:24 | Now, scroll up to locate the output. |
08:28 | The combined list is shown on the Console. |
08:33 | Let us summarize what we have learnt. |
08:37 | In this tutorial, we have learned how to |
08:41 | Create a list in R |
08:43 | Access the elements of a list |
08:46 | Perform indexing on lists |
08:50 | Combine two lists |
08:53 | We now suggest an assignment. |
08:56 | Create a numeric vector c(1:5) and a 5 by 3 matrix with elements from 1 to 15. |
09:04 | Create a named list with vector, matrix and iris data set. |
09:11 | Retrieve the iris data set from the list using dollar operator and indexing method. |
09:18 | State the differences between the results obtained by using dollar operator and indexing method of accessing iris. |
09:28 | For solutions, please refer to the Additional Material section on this website. |
09:35 | The video at the following link summarises the Spoken Tutorial project. |
09:41 | Please download and watch it. |
09:44 | We conduct workshops using Spoken Tutorials and give certificates. |
09:49 | Please contact us. |
09:52 | Please post your timed queries in this forum. |
09:57 | Please post your general queries in this forum. |
10:01 | The FOSSEE team coordinates the TBC project. |
10:05 | For more details, please visit these sites. |
10:08 | The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India |
10:15 | The script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018). |
10:22 | This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching. |