class: center, middle, inverse, title-slide .title[ # STA 326 2.0 Programming and Data Analysis with R ] .subtitle[ ## 🛠️ Data Structures in R ] .author[ ### ] .author[ ### Dr Thiyanga Talagala ] --- <style type="text/css"> .remark-slide-content { font-size: 35px; } </style> # Today's menu .pull-left[ - Vector - Matrix - Array - Data Frame - List ] .pull-right[ <center><img src="menu2.jpeg" height="500px"/></center> ] --- ![](dataStructures.png) Image Credit: venus.ifca.unican.es --- ![](vectors.jpg) Image Credit: venus.ifca.unican.es --- # Recap Write an R code to create the following vector? ``` [1] 1 2 3 4 5 5 4 3 2 1 ``` -- - Method 1 ```r c(1, 2, 3, 4, 5, 5, 4, 3, 2, 1) ``` -- - Method 2 ```r c(1:5, 5:1) ```
02
:
00
--- # Recap - Name elements ```r a <- c(1:5, 5:1) a ``` ``` [1] 1 2 3 4 5 5 4 3 2 1 ``` ```r names(a) <- c("a1", "a2", "a3", "a4", "a5", "b1", "b2", "b3", "b4", "b5") a ``` ``` a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 1 2 3 4 5 5 4 3 2 1 ``` --- ### Operations between vectors ```r a ``` ``` a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 1 2 3 4 5 5 4 3 2 1 ``` ```r a * c(10, 100) ``` ``` a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 10 200 30 400 50 500 40 300 20 100 ``` --- class: inverse, center, middle # Vectors: subsetting **Select some particular elements (i.e., a `subset`) from a vector.** --- ## Vectors: Subsetting ```r myvec <- 1:20; myvec ``` ``` [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` -- ```r myvec[1] ``` ``` [1] 1 ``` -- ```r myvec[5:10] ``` ``` [1] 5 6 7 8 9 10 ``` --- ## Vectors: Subsetting (cont.) -- ```r myvec[-1] ``` ``` [1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` -- ```r myvec[myvec > 3] ``` ``` [1] 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` --- ## vector subsetting - special cases ```r myvec[0] ``` ``` integer(0) ``` ```r myvec[] ``` ``` [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` ```r myvec ``` ``` [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` --- - Extract elements present in vector `a` from `myvec` (cont.). ```r a; myvec ``` ``` a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 1 2 3 4 5 5 4 3 2 1 ``` ``` [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` ```r myvec %in% a ``` ``` [1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ``` ```r myvec[myvec %in% a] ``` ``` [1] 1 2 3 4 5 ``` --- ### Vectors: Subsetting (cont.) ```r myvec ``` ``` [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` ```r b <- 100:105 b ``` ``` [1] 100 101 102 103 104 105 ``` ```r myvec[myvec %in% b] ``` ``` integer(0) ``` --- class: inverse, center, middle # Your turn --- 1. Generate a sequence using the code `seq(from=1, to=10, by=1)`. 2. What other ways can you generate the same sequence? 3. Using the function `rep` , create the below sequence 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4
03
:
00
--- ## Changing values of a vector .pull-left[ ```r covid <- c(100, 30, 40, 50, -1, 100) covid ``` ``` [1] 100 30 40 50 -1 100 ``` ```r covid[1] <- 50000 covid ``` ``` [1] 50000 30 40 50 -1 100 ``` ] -- .pull-right[ ```r covid[covid < 0] <- 0 covid ``` ``` [1] 50000 30 40 50 0 100 ``` ```r covid[c(1, 2)] <- c(1000, 10000) covid ``` ``` [1] 1000 10000 40 50 0 100 ``` ] --- background-image: url('horse.png') background-position: center background-size: cover --- class: inverse, center, middle # 2. Matrices --- background-image: url('dataStructures.png') background-position: center background-size: contain --- Matrix is a 2-dimentional and a homogeneous data structure **Syntax to create a matrix** ```r matrix_name <- matrix(vector_of_elements, nrow=number_of_rows, ncol=number_of_columns, byrow=logical_value, # If byrow=TRUE, then the matrix is filled in by row. dimnames=list(rnames, cnames)) # To assign row names and columns ``` **Example** ```r matrix(1:6, nrow=2, ncol=3) ``` ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` --- cont. ```r matrix(1:6, nrow=2) ``` ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ```r matrix(1:6, ncol=3) ``` ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` --- # Matrix fill by rows/ columns ```r values <- c(10, 20, 30, 40) matrix1 <- matrix(values, nrow=2) # Matrix filled by columns (default option) matrix1 ``` ``` [,1] [,2] [1,] 10 30 [2,] 20 40 ``` ```r matrix2 <- matrix(values, nrow=2, byrow=TRUE) # Matrix filled by rows matrix2 ``` ``` [,1] [,2] [1,] 10 20 [2,] 30 40 ``` --- # Matrix fill by rows/ columns (cont.) - byrow=TRUE: matrix is filled in by row - byrow=FALSE: matrix is filled in by column - Default is by column --- # Naming matrix rows and columns ```r rnames <- c("R1", "R2") cnames <- c("C1", "C2") matrix_with_names <- matrix(values, nrow=2, dimnames=list(rnames, cnames)) matrix_with_names ``` ``` C1 C2 R1 10 30 R2 20 40 ``` --- ## `class` function on vectors vs matrices .pull-left[ ### with vectors ```r vec <- 1:10 class(vec) ``` ``` [1] "integer" ``` ] .pull-right[ ### with matrices ```r mat <- matrix(1:10, ncol=5) class(mat) ``` ``` [1] "matrix" "array" ``` ] --- class: inverse, center, middle # Matrix: subsetting --- # Matrix: subsetting .pull-left[ ```r matrix1 ``` ``` [,1] [,2] [1,] 10 30 [2,] 20 40 ``` ] .pull-right[ `matraix_name[i, ]` gives the ith row of a matrix ```r matrix1[1, ] ``` ``` [1] 10 30 ``` `matraix_name[, j]` gives the jth column of a matrix ```r matrix1[, 2] ``` ``` [1] 30 40 ``` ] --- # Matrix: subsetting (cont.) `matraix_name[i, j]` gives the ith row and jth column element .pull-left[ ```r matrix1 ``` ``` [,1] [,2] [1,] 10 30 [2,] 20 40 ``` ] .pull-right[ ```r matrix1[1, 2] ``` ``` [1] 30 ``` ```r matrix1[1, c(1, 2)] ``` ``` [1] 10 30 ``` ] --- **Beware!** .pull-left[ ```r amat <- matrix(10:90, 3, 3); amat ``` ``` [,1] [,2] [,3] [1,] 10 13 16 [2,] 11 14 17 [3,] 12 15 18 ``` ```r bmat <- amat[1:2,]; bmat ``` ``` [,1] [,2] [,3] [1,] 10 13 16 [2,] 11 14 17 ``` ```r class(bmat) ``` ``` [1] "matrix" "array" ``` ] -- .pull-right[ ```r cmat <- amat[1,]; cmat ``` ``` [1] 10 13 16 ``` ```r class(cmat) ``` ``` [1] "integer" ``` ```r dmat <- amat[, 1]; dmat ``` ``` [1] 10 11 12 ``` ```r class(dmat) ``` ``` [1] "integer" ``` ] --- **drop = FALSE** .pull-left[ ```r cmat <- amat[1,]; cmat ``` ``` [1] 10 13 16 ``` ```r class(cmat) ``` ``` [1] "integer" ``` ] .pull-right[ ```r cmat <- amat[1, , drop = FALSE]; cmat ``` ``` [,1] [,2] [,3] [1,] 10 13 16 ``` ```r class(cmat) ``` ``` [1] "matrix" "array" ``` ] --- ## Diagonal elements .pull-left[ ```r a <- matrix(1:9, ncol=3) a ``` ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ] .pull-right[ ```r diag(a) ``` ``` [1] 1 5 9 ``` ] --- ## Replacing elements in a matrix .pull-left[ ```r a <- matrix(1:9, ncol=3) a ``` ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ```r a[1, 1] <- 100 a ``` ``` [,1] [,2] [,3] [1,] 100 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ] .pull-right[ ```r diag(a) <- 0 a ``` ``` [,1] [,2] [,3] [1,] 0 4 7 [2,] 2 0 8 [3,] 3 6 0 ``` ] --- # `cbind` and `rbind` Matrices can be created by column-binding and row-binding with `cbind()` and `rbind()` ```r x <- 1:3 y <- c(10, 100, 1000) cbind(x, y) # binds matrices horizontally ``` ``` x y [1,] 1 10 [2,] 2 100 [3,] 3 1000 ``` ```r rbind(x, y) #binds matrices vertically ``` ``` [,1] [,2] [,3] x 1 2 3 y 10 100 1000 ``` --- # `cbind` and `rbind` (cont.) .pull-left[ ```r a <- matrix(1:3, ncol=3) a ``` ``` [,1] [,2] [,3] [1,] 1 2 3 ``` ```r b <- matrix(c(10, 100, 1000), ncol=3) b ``` ``` [,1] [,2] [,3] [1,] 10 100 1000 ``` ] .pull-right[ ```r cbind(a, b) # binds matrices horizontally ``` ``` [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 2 3 10 100 1000 ``` ```r rbind(a, b) #binds matrices vertically ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 10 100 1000 ``` ] --- class: inverse, center, middle # Matrix operations --- # Matrix operations Transpose ```r t(x) ``` ``` [,1] [,2] [,3] [1,] 1 2 3 ``` Matrix multiplication ```r y <- matrix(seq(10, 60, by=10), nrow=3) z <- x %*% y z ``` ``` [,1] [,2] [1,] 140 320 ``` --- # Matrix operations (cont.) - matrix inverse is solve() Find x in: m*x=n ```r solve(m, n) ``` --- class: inverse, center, middle # Your turn --- background-image: url('paper.png') background-position: center background-size: contain
05
:
00
--- Help: `$$\hat{\beta}=(X^TX)^{-1}X^TY$$` --- background-image: url('slr.png') background-position: center background-size: contain
05
:
00
--- .pull-left[ ## Logical operators with matrices ```r a <- matrix(c(1:12), nrow = 3, ncol = 4) a > 10 ``` ``` [,1] [,2] [,3] [,4] [1,] FALSE FALSE FALSE FALSE [2,] FALSE FALSE FALSE TRUE [3,] FALSE FALSE FALSE TRUE ``` ```r b <- matrix(1:4, ncol=4) ``` ```r a > b # Error ``` ] .pull-right[ ## %in% operator ```r b %in% a ``` ``` [1] TRUE TRUE TRUE TRUE ``` ```r a %in% b ``` ``` [1] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ``` ```r class(a %in% b) ``` ``` [1] "logical" ``` ] --- class: inverse, center, middle # 3. Arrays ![](arraynew.png) --- ## Array - data structures for storing **higher** dimensional data. - a **homogeneous** data structure. - a special case of the array is the matrix. ```r array(vector, dimensions, dimnames) #dimnames-as a list ``` ```r a <- array(c(10, 20, 30, 40, 50, 60), c(1, 2, 3)) ``` --- ```r a <- array(c(10, 20, 30, 40, 50, 60), c(1, 2, 3)) a ``` ``` , , 1 [,1] [,2] [1,] 10 20 , , 2 [,1] [,2] [1,] 30 40 , , 3 [,1] [,2] [1,] 50 60 ``` --- background-image: url('array.png') background-position: center background-size: contain --- ## Subsetting arrays .pull-left[ ```r a ``` ``` , , 1 [,1] [,2] [1,] 10 20 , , 2 [,1] [,2] [1,] 30 40 , , 3 [,1] [,2] [1,] 50 60 ``` ] .pull-right[ ```r a[1, 2, 3] ``` ``` [1] 60 ``` ```r a[, , 1] # Extract first entry ``` ``` [1] 10 20 ``` ] --- ## Subsetting arrays (cont.) .pull-left[ ```r a ``` ``` , , 1 [,1] [,2] [1,] 10 20 , , 2 [,1] [,2] [1,] 30 40 , , 3 [,1] [,2] [1,] 50 60 ``` ] .pull-right[ ```r a[1, ,] # All rows in each entry ``` ``` [,1] [,2] [,3] [1,] 10 30 50 [2,] 20 40 60 ``` ] --- class: inverse, center, middle # Your turn --- 1. Create the following matrix using the `array` function ```r matrix(1:20, ncol=5) ``` ``` [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 ```
02
:
00
--- ## Array with dimnames ```r dim1 <- c("A1", "A2"); dim2 <- c("B1", "B2", "B3"); dim3 <- c("c1", "c2", "c3", "c4") z <- array(1:24, c(2, 3, 4), dimnames = list(dim1, dim2, dim3)) z ``` ``` , , c1 B1 B2 B3 A1 1 3 5 A2 2 4 6 , , c2 B1 B2 B3 A1 7 9 11 A2 8 10 12 , , c3 B1 B2 B3 A1 13 15 17 A2 14 16 18 , , c4 B1 B2 B3 A1 19 21 23 A2 20 22 24 ``` --- .pull-left[ ```r dim1 <- c("A1", "A2"); dim2 <- c("B1", "B2", "B3"); dim3 <- c("c1", "c2", "c3", "c4") z <- array(1:24, c(2, 3, 4), dimnames = list(dim1, dim2, dim3)) z ``` ] .pull-right[ ``` ## , , c1 ## ## B1 B2 B3 ## A1 1 3 5 ## A2 2 4 6 ## ## , , c2 ## ## B1 B2 B3 ## A1 7 9 11 ## A2 8 10 12 ## ## , , c3 ## ## B1 B2 B3 ## A1 13 15 17 ## A2 14 16 18 ## ## , , c4 ## ## B1 B2 B3 ## A1 19 21 23 ## A2 20 22 24 ``` ] --- ![](applicationarray.png) --- class: inverse, center, middle # 4. Data Frames ![](dfnew.png) --- # Data frames - Rectangular arrangement of data with rows corresponding to observational units and columns corresponding to variables. - More general than a matrix in that different columns can contain different modes of data. - It’s similar to the datasets you’d typically see in SPSS and MINITAB. - Data frames are the most common data structure you’ll deal with in R. --- ![Figure 1: Components of a dataframe.](tidy-1.png) Image Credit: Hadley Wickham --- ## Create a data frame **Syntax** ```r name_of_the_dataframe <- data.frame( var1_name=vector of values of the first variable, var2_names=vector of values of the second variable) ``` **Example** ```r corona <- data.frame(ID=c("C001", "C002", "C003", "C004"), Location=c("Beijing", "Wuhan", "Shanghai", "Beijing"), Test_Results=c(FALSE, TRUE, FALSE, FALSE)) corona ``` ``` ID Location Test_Results 1 C001 Beijing FALSE 2 C002 Wuhan TRUE 3 C003 Shanghai FALSE 4 C004 Beijing FALSE ``` To check if it is a dataframe ```r is.data.frame(corona) ``` ``` [1] TRUE ``` --- # Some useful functions with dataframes ```r colnames(corona) ``` ``` [1] "ID" "Location" "Test_Results" ``` ```r length(corona) ``` ``` [1] 3 ``` ```r dim(corona) ``` ``` [1] 4 3 ``` ```r nrow(corona) ``` ``` [1] 4 ``` ```r ncol(corona) ``` ``` [1] 3 ``` --- # Some useful functions with dataframes (cont.) ```r summary(corona) ``` ``` ID Location Test_Results Length:4 Length:4 Mode :logical Class :character Class :character FALSE:3 Mode :character Mode :character TRUE :1 ``` ```r str(corona) ``` ``` 'data.frame': 4 obs. of 3 variables: $ ID : chr "C001" "C002" "C003" "C004" $ Location : chr "Beijing" "Wuhan" "Shanghai" "Beijing" $ Test_Results: logi FALSE TRUE FALSE FALSE ``` --- ## Convert a matrix to a dataframe ```r mat <- matrix(1:16, ncol=4) mat ``` ``` [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 ``` ```r mat_df <- as.data.frame(mat) mat_df ``` ``` V1 V2 V3 V4 1 1 5 9 13 2 2 6 10 14 3 3 7 11 15 4 4 8 12 16 ``` --- ## Subsetting data frames **Select rows** .pull-left[ ```r head(mat_df) # default it shows 6 rows ``` ``` V1 V2 V3 V4 1 1 5 9 13 2 2 6 10 14 3 3 7 11 15 4 4 8 12 16 ``` ```r head(mat_df, 3) # To extract only the first three rows ``` ``` V1 V2 V3 V4 1 1 5 9 13 2 2 6 10 14 3 3 7 11 15 ``` ] .pull-right[ ```r tail(mat_df, 2) ``` ``` V1 V2 V3 V4 3 3 7 11 15 4 4 8 12 16 ``` ] --- ## Subsetting data frames .pull-left[ ```r mat_df ``` ``` V1 V2 V3 V4 1 1 5 9 13 2 2 6 10 14 3 3 7 11 15 4 4 8 12 16 ``` ] .pull-right[ **To select some specific rows** ```r mat_df[4, ] ``` ``` V1 V2 V3 V4 4 4 8 12 16 ``` ```r index <- c(1, 3) mat_df[index, ] ``` ``` V1 V2 V3 V4 1 1 5 9 13 3 3 7 11 15 ``` ] --- ## Subsetting data frames: select columns .pull-left[ ```r mat_df ``` ``` V1 V2 V3 V4 1 1 5 9 13 2 2 6 10 14 3 3 7 11 15 4 4 8 12 16 ``` ] .pull-right[ Select column(s) by variable names ```r mat_df$V1 # Method 1 ``` ``` [1] 1 2 3 4 ``` ```r mat_df[, "V1"] # Method 2 ``` ``` [1] 1 2 3 4 ``` ] --- ## Subsetting data frames: select columns .pull-left[ ```r mat_df ``` ``` V1 V2 V3 V4 1 1 5 9 13 2 2 6 10 14 3 3 7 11 15 4 4 8 12 16 ``` ] .pull-right[ Select column(s) by index ```r mat_df[, 2] ``` ``` [1] 5 6 7 8 ``` ] --- class: inverse, center, middle # Your turn --- background-image: url('datafr.png') background-position: center background-size: contain
03
:
00
--- background-image: url('iris.jpg') background-position: center background-size: cover **Built-in dataframes** --- ## Built-in dataframes ```r data(iris) ``` .full-width[.content-box-blue[Use `help` function to find more about the iris dataset.]] --- background-image: url('helpiris.png') background-position: center background-size: contain --- # `iris` dataset ```r head(iris) ``` ``` Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa ``` <img src="iris_flower_dataset.png" width="800"> --- ```r head(iris) ``` ``` Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa ``` <img src="images.jpeg" width="300"><img src="iris_petal_sepal.png" height="300"> --- ```r head(iris) ``` ``` Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa ``` - Introduced by Sir Ronald A. Fisher, 1936 <img src="Fisher.jpeg" height="300"> --- Use the R dataset “iris” to answer the following questions: 1. How many rows and columns does iris have? 2. Select the first 4 rows. 3. Select the last 6 rows. 4. Select rows 10 to 20, with all columns in the iris dataset. 5. Select rows 10 to 20 with only the Species, Petal.Width and Petal.Length.
05
:
00
--- cont. 6. Create a single vector (a new object) called ‘width’ that is the Sepal.Width column of iris. 7. What are the column names and data types of the different columns in iris? 8. How many rows in the iris dataset have `Petal.Length` larger than 5 and `Sepal.Width` smaller than 3?
05
:
00
--- class: inverse, center, middle # 5. List --- background-image: url('dataStructures.png') background-position: center background-size: contain --- # List - Lists are heterogeneous **Syntax** ```r list_name <- list(entry1, entry2, entry3, ...) ``` **Example** ```r first_list <-list(1:3, matrix(1:6, nrow=2), iris) ``` --- ## List (str) ```r first_list <-list(1:3, matrix(1:6, nrow=2), iris) first_list ``` ``` [[1]] [1] 1 2 3 [[2]] [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [[3]] Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa 7 4.6 3.4 1.4 0.3 setosa 8 5.0 3.4 1.5 0.2 setosa 9 4.4 2.9 1.4 0.2 setosa 10 4.9 3.1 1.5 0.1 setosa 11 5.4 3.7 1.5 0.2 setosa 12 4.8 3.4 1.6 0.2 setosa 13 4.8 3.0 1.4 0.1 setosa 14 4.3 3.0 1.1 0.1 setosa 15 5.8 4.0 1.2 0.2 setosa 16 5.7 4.4 1.5 0.4 setosa 17 5.4 3.9 1.3 0.4 setosa 18 5.1 3.5 1.4 0.3 setosa 19 5.7 3.8 1.7 0.3 setosa 20 5.1 3.8 1.5 0.3 setosa 21 5.4 3.4 1.7 0.2 setosa 22 5.1 3.7 1.5 0.4 setosa 23 4.6 3.6 1.0 0.2 setosa 24 5.1 3.3 1.7 0.5 setosa 25 4.8 3.4 1.9 0.2 setosa 26 5.0 3.0 1.6 0.2 setosa 27 5.0 3.4 1.6 0.4 setosa 28 5.2 3.5 1.5 0.2 setosa 29 5.2 3.4 1.4 0.2 setosa 30 4.7 3.2 1.6 0.2 setosa 31 4.8 3.1 1.6 0.2 setosa 32 5.4 3.4 1.5 0.4 setosa 33 5.2 4.1 1.5 0.1 setosa 34 5.5 4.2 1.4 0.2 setosa 35 4.9 3.1 1.5 0.2 setosa 36 5.0 3.2 1.2 0.2 setosa 37 5.5 3.5 1.3 0.2 setosa 38 4.9 3.6 1.4 0.1 setosa 39 4.4 3.0 1.3 0.2 setosa 40 5.1 3.4 1.5 0.2 setosa 41 5.0 3.5 1.3 0.3 setosa 42 4.5 2.3 1.3 0.3 setosa 43 4.4 3.2 1.3 0.2 setosa 44 5.0 3.5 1.6 0.6 setosa 45 5.1 3.8 1.9 0.4 setosa 46 4.8 3.0 1.4 0.3 setosa 47 5.1 3.8 1.6 0.2 setosa 48 4.6 3.2 1.4 0.2 setosa 49 5.3 3.7 1.5 0.2 setosa 50 5.0 3.3 1.4 0.2 setosa 51 7.0 3.2 4.7 1.4 versicolor 52 6.4 3.2 4.5 1.5 versicolor 53 6.9 3.1 4.9 1.5 versicolor 54 5.5 2.3 4.0 1.3 versicolor 55 6.5 2.8 4.6 1.5 versicolor 56 5.7 2.8 4.5 1.3 versicolor 57 6.3 3.3 4.7 1.6 versicolor 58 4.9 2.4 3.3 1.0 versicolor 59 6.6 2.9 4.6 1.3 versicolor 60 5.2 2.7 3.9 1.4 versicolor 61 5.0 2.0 3.5 1.0 versicolor 62 5.9 3.0 4.2 1.5 versicolor 63 6.0 2.2 4.0 1.0 versicolor 64 6.1 2.9 4.7 1.4 versicolor 65 5.6 2.9 3.6 1.3 versicolor 66 6.7 3.1 4.4 1.4 versicolor 67 5.6 3.0 4.5 1.5 versicolor 68 5.8 2.7 4.1 1.0 versicolor 69 6.2 2.2 4.5 1.5 versicolor 70 5.6 2.5 3.9 1.1 versicolor 71 5.9 3.2 4.8 1.8 versicolor 72 6.1 2.8 4.0 1.3 versicolor 73 6.3 2.5 4.9 1.5 versicolor 74 6.1 2.8 4.7 1.2 versicolor 75 6.4 2.9 4.3 1.3 versicolor 76 6.6 3.0 4.4 1.4 versicolor 77 6.8 2.8 4.8 1.4 versicolor 78 6.7 3.0 5.0 1.7 versicolor 79 6.0 2.9 4.5 1.5 versicolor 80 5.7 2.6 3.5 1.0 versicolor 81 5.5 2.4 3.8 1.1 versicolor 82 5.5 2.4 3.7 1.0 versicolor 83 5.8 2.7 3.9 1.2 versicolor 84 6.0 2.7 5.1 1.6 versicolor 85 5.4 3.0 4.5 1.5 versicolor 86 6.0 3.4 4.5 1.6 versicolor 87 6.7 3.1 4.7 1.5 versicolor 88 6.3 2.3 4.4 1.3 versicolor 89 5.6 3.0 4.1 1.3 versicolor 90 5.5 2.5 4.0 1.3 versicolor 91 5.5 2.6 4.4 1.2 versicolor 92 6.1 3.0 4.6 1.4 versicolor 93 5.8 2.6 4.0 1.2 versicolor 94 5.0 2.3 3.3 1.0 versicolor 95 5.6 2.7 4.2 1.3 versicolor 96 5.7 3.0 4.2 1.2 versicolor 97 5.7 2.9 4.2 1.3 versicolor 98 6.2 2.9 4.3 1.3 versicolor 99 5.1 2.5 3.0 1.1 versicolor 100 5.7 2.8 4.1 1.3 versicolor 101 6.3 3.3 6.0 2.5 virginica 102 5.8 2.7 5.1 1.9 virginica 103 7.1 3.0 5.9 2.1 virginica 104 6.3 2.9 5.6 1.8 virginica 105 6.5 3.0 5.8 2.2 virginica 106 7.6 3.0 6.6 2.1 virginica 107 4.9 2.5 4.5 1.7 virginica 108 7.3 2.9 6.3 1.8 virginica 109 6.7 2.5 5.8 1.8 virginica 110 7.2 3.6 6.1 2.5 virginica 111 6.5 3.2 5.1 2.0 virginica 112 6.4 2.7 5.3 1.9 virginica 113 6.8 3.0 5.5 2.1 virginica 114 5.7 2.5 5.0 2.0 virginica 115 5.8 2.8 5.1 2.4 virginica 116 6.4 3.2 5.3 2.3 virginica 117 6.5 3.0 5.5 1.8 virginica 118 7.7 3.8 6.7 2.2 virginica 119 7.7 2.6 6.9 2.3 virginica 120 6.0 2.2 5.0 1.5 virginica 121 6.9 3.2 5.7 2.3 virginica 122 5.6 2.8 4.9 2.0 virginica 123 7.7 2.8 6.7 2.0 virginica 124 6.3 2.7 4.9 1.8 virginica 125 6.7 3.3 5.7 2.1 virginica 126 7.2 3.2 6.0 1.8 virginica 127 6.2 2.8 4.8 1.8 virginica 128 6.1 3.0 4.9 1.8 virginica 129 6.4 2.8 5.6 2.1 virginica 130 7.2 3.0 5.8 1.6 virginica 131 7.4 2.8 6.1 1.9 virginica 132 7.9 3.8 6.4 2.0 virginica 133 6.4 2.8 5.6 2.2 virginica 134 6.3 2.8 5.1 1.5 virginica 135 6.1 2.6 5.6 1.4 virginica 136 7.7 3.0 6.1 2.3 virginica 137 6.3 3.4 5.6 2.4 virginica 138 6.4 3.1 5.5 1.8 virginica 139 6.0 3.0 4.8 1.8 virginica 140 6.9 3.1 5.4 2.1 virginica 141 6.7 3.1 5.6 2.4 virginica 142 6.9 3.1 5.1 2.3 virginica 143 5.8 2.7 5.1 1.9 virginica 144 6.8 3.2 5.9 2.3 virginica 145 6.7 3.3 5.7 2.5 virginica 146 6.7 3.0 5.2 2.3 virginica 147 6.3 2.5 5.0 1.9 virginica 148 6.5 3.0 5.2 2.0 virginica 149 6.2 3.4 5.4 2.3 virginica 150 5.9 3.0 5.1 1.8 virginica ``` --- ## Structure of a list ```r str(first_list) ``` ``` List of 3 $ : int [1:3] 1 2 3 $ : int [1:2, 1:3] 1 2 3 4 5 6 $ :'data.frame': 150 obs. of 5 variables: ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... ``` -- ## Extract elements ```r first_list[[1]] ``` ``` [1] 1 2 3 ``` ```r first_list[[3]]$Species ``` ``` [1] setosa setosa setosa setosa setosa setosa [7] setosa setosa setosa setosa setosa setosa [13] setosa setosa setosa setosa setosa setosa [19] setosa setosa setosa setosa setosa setosa [25] setosa setosa setosa setosa setosa setosa [31] setosa setosa setosa setosa setosa setosa [37] setosa setosa setosa setosa setosa setosa [43] setosa setosa setosa setosa setosa setosa [49] setosa setosa versicolor versicolor versicolor versicolor [55] versicolor versicolor versicolor versicolor versicolor versicolor [61] versicolor versicolor versicolor versicolor versicolor versicolor [67] versicolor versicolor versicolor versicolor versicolor versicolor [73] versicolor versicolor versicolor versicolor versicolor versicolor [79] versicolor versicolor versicolor versicolor versicolor versicolor [85] versicolor versicolor versicolor versicolor versicolor versicolor [91] versicolor versicolor versicolor versicolor versicolor versicolor [97] versicolor versicolor versicolor versicolor virginica virginica [103] virginica virginica virginica virginica virginica virginica [109] virginica virginica virginica virginica virginica virginica [115] virginica virginica virginica virginica virginica virginica [121] virginica virginica virginica virginica virginica virginica [127] virginica virginica virginica virginica virginica virginica [133] virginica virginica virginica virginica virginica virginica [139] virginica virginica virginica virginica virginica virginica [145] virginica virginica virginica virginica virginica virginica Levels: setosa versicolor virginica ``` --- # Name entries in a list ```r first_list_with_names <-list(a=1:3, b=matrix(1:6, nrow=2), c=iris) first_list_with_names ``` ``` $a [1] 1 2 3 $b [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 $c Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa 7 4.6 3.4 1.4 0.3 setosa 8 5.0 3.4 1.5 0.2 setosa 9 4.4 2.9 1.4 0.2 setosa 10 4.9 3.1 1.5 0.1 setosa 11 5.4 3.7 1.5 0.2 setosa 12 4.8 3.4 1.6 0.2 setosa 13 4.8 3.0 1.4 0.1 setosa 14 4.3 3.0 1.1 0.1 setosa 15 5.8 4.0 1.2 0.2 setosa 16 5.7 4.4 1.5 0.4 setosa 17 5.4 3.9 1.3 0.4 setosa 18 5.1 3.5 1.4 0.3 setosa 19 5.7 3.8 1.7 0.3 setosa 20 5.1 3.8 1.5 0.3 setosa 21 5.4 3.4 1.7 0.2 setosa 22 5.1 3.7 1.5 0.4 setosa 23 4.6 3.6 1.0 0.2 setosa 24 5.1 3.3 1.7 0.5 setosa 25 4.8 3.4 1.9 0.2 setosa 26 5.0 3.0 1.6 0.2 setosa 27 5.0 3.4 1.6 0.4 setosa 28 5.2 3.5 1.5 0.2 setosa 29 5.2 3.4 1.4 0.2 setosa 30 4.7 3.2 1.6 0.2 setosa 31 4.8 3.1 1.6 0.2 setosa 32 5.4 3.4 1.5 0.4 setosa 33 5.2 4.1 1.5 0.1 setosa 34 5.5 4.2 1.4 0.2 setosa 35 4.9 3.1 1.5 0.2 setosa 36 5.0 3.2 1.2 0.2 setosa 37 5.5 3.5 1.3 0.2 setosa 38 4.9 3.6 1.4 0.1 setosa 39 4.4 3.0 1.3 0.2 setosa 40 5.1 3.4 1.5 0.2 setosa 41 5.0 3.5 1.3 0.3 setosa 42 4.5 2.3 1.3 0.3 setosa 43 4.4 3.2 1.3 0.2 setosa 44 5.0 3.5 1.6 0.6 setosa 45 5.1 3.8 1.9 0.4 setosa 46 4.8 3.0 1.4 0.3 setosa 47 5.1 3.8 1.6 0.2 setosa 48 4.6 3.2 1.4 0.2 setosa 49 5.3 3.7 1.5 0.2 setosa 50 5.0 3.3 1.4 0.2 setosa 51 7.0 3.2 4.7 1.4 versicolor 52 6.4 3.2 4.5 1.5 versicolor 53 6.9 3.1 4.9 1.5 versicolor 54 5.5 2.3 4.0 1.3 versicolor 55 6.5 2.8 4.6 1.5 versicolor 56 5.7 2.8 4.5 1.3 versicolor 57 6.3 3.3 4.7 1.6 versicolor 58 4.9 2.4 3.3 1.0 versicolor 59 6.6 2.9 4.6 1.3 versicolor 60 5.2 2.7 3.9 1.4 versicolor 61 5.0 2.0 3.5 1.0 versicolor 62 5.9 3.0 4.2 1.5 versicolor 63 6.0 2.2 4.0 1.0 versicolor 64 6.1 2.9 4.7 1.4 versicolor 65 5.6 2.9 3.6 1.3 versicolor 66 6.7 3.1 4.4 1.4 versicolor 67 5.6 3.0 4.5 1.5 versicolor 68 5.8 2.7 4.1 1.0 versicolor 69 6.2 2.2 4.5 1.5 versicolor 70 5.6 2.5 3.9 1.1 versicolor 71 5.9 3.2 4.8 1.8 versicolor 72 6.1 2.8 4.0 1.3 versicolor 73 6.3 2.5 4.9 1.5 versicolor 74 6.1 2.8 4.7 1.2 versicolor 75 6.4 2.9 4.3 1.3 versicolor 76 6.6 3.0 4.4 1.4 versicolor 77 6.8 2.8 4.8 1.4 versicolor 78 6.7 3.0 5.0 1.7 versicolor 79 6.0 2.9 4.5 1.5 versicolor 80 5.7 2.6 3.5 1.0 versicolor 81 5.5 2.4 3.8 1.1 versicolor 82 5.5 2.4 3.7 1.0 versicolor 83 5.8 2.7 3.9 1.2 versicolor 84 6.0 2.7 5.1 1.6 versicolor 85 5.4 3.0 4.5 1.5 versicolor 86 6.0 3.4 4.5 1.6 versicolor 87 6.7 3.1 4.7 1.5 versicolor 88 6.3 2.3 4.4 1.3 versicolor 89 5.6 3.0 4.1 1.3 versicolor 90 5.5 2.5 4.0 1.3 versicolor 91 5.5 2.6 4.4 1.2 versicolor 92 6.1 3.0 4.6 1.4 versicolor 93 5.8 2.6 4.0 1.2 versicolor 94 5.0 2.3 3.3 1.0 versicolor 95 5.6 2.7 4.2 1.3 versicolor 96 5.7 3.0 4.2 1.2 versicolor 97 5.7 2.9 4.2 1.3 versicolor 98 6.2 2.9 4.3 1.3 versicolor 99 5.1 2.5 3.0 1.1 versicolor 100 5.7 2.8 4.1 1.3 versicolor 101 6.3 3.3 6.0 2.5 virginica 102 5.8 2.7 5.1 1.9 virginica 103 7.1 3.0 5.9 2.1 virginica 104 6.3 2.9 5.6 1.8 virginica 105 6.5 3.0 5.8 2.2 virginica 106 7.6 3.0 6.6 2.1 virginica 107 4.9 2.5 4.5 1.7 virginica 108 7.3 2.9 6.3 1.8 virginica 109 6.7 2.5 5.8 1.8 virginica 110 7.2 3.6 6.1 2.5 virginica 111 6.5 3.2 5.1 2.0 virginica 112 6.4 2.7 5.3 1.9 virginica 113 6.8 3.0 5.5 2.1 virginica 114 5.7 2.5 5.0 2.0 virginica 115 5.8 2.8 5.1 2.4 virginica 116 6.4 3.2 5.3 2.3 virginica 117 6.5 3.0 5.5 1.8 virginica 118 7.7 3.8 6.7 2.2 virginica 119 7.7 2.6 6.9 2.3 virginica 120 6.0 2.2 5.0 1.5 virginica 121 6.9 3.2 5.7 2.3 virginica 122 5.6 2.8 4.9 2.0 virginica 123 7.7 2.8 6.7 2.0 virginica 124 6.3 2.7 4.9 1.8 virginica 125 6.7 3.3 5.7 2.1 virginica 126 7.2 3.2 6.0 1.8 virginica 127 6.2 2.8 4.8 1.8 virginica 128 6.1 3.0 4.9 1.8 virginica 129 6.4 2.8 5.6 2.1 virginica 130 7.2 3.0 5.8 1.6 virginica 131 7.4 2.8 6.1 1.9 virginica 132 7.9 3.8 6.4 2.0 virginica 133 6.4 2.8 5.6 2.2 virginica 134 6.3 2.8 5.1 1.5 virginica 135 6.1 2.6 5.6 1.4 virginica 136 7.7 3.0 6.1 2.3 virginica 137 6.3 3.4 5.6 2.4 virginica 138 6.4 3.1 5.5 1.8 virginica 139 6.0 3.0 4.8 1.8 virginica 140 6.9 3.1 5.4 2.1 virginica 141 6.7 3.1 5.6 2.4 virginica 142 6.9 3.1 5.1 2.3 virginica 143 5.8 2.7 5.1 1.9 virginica 144 6.8 3.2 5.9 2.3 virginica 145 6.7 3.3 5.7 2.5 virginica 146 6.7 3.0 5.2 2.3 virginica 147 6.3 2.5 5.0 1.9 virginica 148 6.5 3.0 5.2 2.0 virginica 149 6.2 3.4 5.4 2.3 virginica 150 5.9 3.0 5.1 1.8 virginica ``` --- ## Extract elements using names ```r str(first_list_with_names) ``` ``` List of 3 $ a: int [1:3] 1 2 3 $ b: int [1:2, 1:3] 1 2 3 4 5 6 $ c:'data.frame': 150 obs. of 5 variables: ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... ``` ```r first_list_with_names$a ``` ``` [1] 1 2 3 ``` --- ```r first_list_with_names$c$Species ``` ``` [1] setosa setosa setosa setosa setosa setosa [7] setosa setosa setosa setosa setosa setosa [13] setosa setosa setosa setosa setosa setosa [19] setosa setosa setosa setosa setosa setosa [25] setosa setosa setosa setosa setosa setosa [31] setosa setosa setosa setosa setosa setosa [37] setosa setosa setosa setosa setosa setosa [43] setosa setosa setosa setosa setosa setosa [49] setosa setosa versicolor versicolor versicolor versicolor [55] versicolor versicolor versicolor versicolor versicolor versicolor [61] versicolor versicolor versicolor versicolor versicolor versicolor [67] versicolor versicolor versicolor versicolor versicolor versicolor [73] versicolor versicolor versicolor versicolor versicolor versicolor [79] versicolor versicolor versicolor versicolor versicolor versicolor [85] versicolor versicolor versicolor versicolor versicolor versicolor [91] versicolor versicolor versicolor versicolor versicolor versicolor [97] versicolor versicolor versicolor versicolor virginica virginica [103] virginica virginica virginica virginica virginica virginica [109] virginica virginica virginica virginica virginica virginica [115] virginica virginica virginica virginica virginica virginica [121] virginica virginica virginica virginica virginica virginica [127] virginica virginica virginica virginica virginica virginica [133] virginica virginica virginica virginica virginica virginica [139] virginica virginica virginica virginica virginica virginica [145] virginica virginica virginica virginica virginica virginica Levels: setosa versicolor virginica ``` --- class: inverse, center, middle # Your turn --- ```r c("Jan","Feb","Mar"); matrix(c(3,9,5,1,-2,8), nrow = 2); list("green",12.3) ``` ``` [1] "Jan" "Feb" "Mar" ``` ``` [,1] [,2] [,3] [1,] 3 5 -2 [2,] 9 1 8 ``` ``` [[1]] [1] "green" [[2]] [1] 12.3 ``` 1. Create a list containing the above vector, matrix and the list. 2. Name the elements as `first`, `second` and `third`. --- class: center, middle ## Thank you! Slides available at: hellor.netlify.app All rights reserved by [Thiyanga S. Talagala](https://thiyanga.netlify.app/)