class: center, middle, inverse, title-slide # Introduction into R ##
### Johan Aparicio ### International Center for Tropical Agriculture ### 2019/10/09 --- class: center, middle # Recordando... --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Basic operations ```r a <- 3+3 b <- 3*4 c <- 4^2 ``` -- ```r a + b + c ``` ``` [1] 34 ``` -- ```r z <- log(200) w <- exp(4) x <- sqrt(9) ``` -- ```r z - w + x ``` ``` [1] -46.29983 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Hello World En R existen varios tipos de objectos que permiten que el usuario pueda almacenar la información para realizar procedimientos estadísticos y gráficos. Los principales objetos en R son: - [Vectores](vectores) - [Matrices](matrices) - [Marcos de datos](marcos de datos) - [Listas](listas) --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Vectors <center> <img src='images/r-vector.jpg' alt='RStudio' width='100%'> </center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Vectors Vectors are ordered arrangements in which information of numerical type **([quantitative variable](quantitative variable))**, alphanumeric **([qualitative variable](qualitative variable))** or logical **([TRUE or FALSE](TRUE or FALSE))** can be stored, but not mixtures of these. <br> -- <center> <img src="images/example_vect.PNG" width="40%"> </center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% ###Numeric ([Quantitative](Quantitative)) ```r a <- 41201 ; b <- 12.3 ``` -- ----------------- ###Character ([Qualitative](Quanlitative)) ```r e <- "Dog" ; f <- "CIAT" ; g <- "RStudio" ``` -- ----------------- ### Boolean ([Logical](Logical)) ```r TRUE FALSE ``` ```r r <- 4 > 6 ; w <- 400 == 30 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Class ```r a <- 41201 class(a) ``` ``` [1] "numeric" ``` -- ----------------- ```r g <- "RStudio" class(g) ``` ``` [1] "character" ``` -- ----------------- ```r r <- 4 > 6 class(r) ``` ``` [1] "logical" ``` --- background-image: url(cheat.PNG) background-size: 700px --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Example <center> <img src="images/seq2.gif" width="100%"> </center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Vectors using colon operator ```r # Creating a sequence from 5 to 13. v <- 5:13 *print(v) ``` ``` [1] 5 6 7 8 9 10 11 12 13 ``` -- ```r # Creating a sequence from 6.6 to 12.6. v <- 6.6:12.6 print(v) ``` ``` [1] 6.6 7.6 8.6 9.6 10.6 11.6 12.6 ``` -- ```r # What happens in this case? *v <- 3.8:11.4 print(v) ``` -- ``` [1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% class: middle ##Using sequence (seq.) operator ```r # Create vector with elements from 5 to 9 incrementing by 0.4. test <- seq(from = 5, to = 9, by = 0.4) ``` -- ```r print(test) ``` ``` [1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0 ``` -- ```r # What happens in this case? seq( from = 2 , to = 7 , by = 0.7) ``` -- ``` [1] 2.0 2.7 3.4 4.1 4.8 5.5 6.2 6.9 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% #Using the c( ) function ```r x <- c(1, 5, 4, 9, 0) y <- c("Carlos", "Bean" , "Carolina Torres") z <- c(TRUE, FALSE , FALSE) ``` ----------------- -- ```r class(x) ``` ``` [1] "numeric" ``` ```r class(y) ``` ``` [1] "character" ``` ```r class(z) ``` ``` [1] "logical" ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% ##Giving different types of data items in a Vector ```r y <- c(2,3,"ten") ``` -- Since vector is a collection of similar data type. Therefore, if we try to create vectors like the above example then Coercion takes place. -- ```r y ``` ``` [1] "2" "3" "ten" ``` -- ## Generic order in increasing fashion is: <center> Boolean < Numeric < Character </center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% ```r # Example 1 x <- c(3,4,5,"ten",TRUE) ``` -- ```r x ``` ``` [1] "3" "4" "5" "ten" "TRUE" ``` ------------------- -- ```r # Example 2 p <- c(3,4,5,TRUE) ``` -- ```r p ``` ``` [1] 3 4 5 1 ``` ------------------- -- ```r # Example 3 q <- c("car", FALSE, TRUE) ``` -- ```r q ``` ``` [1] "car" "FALSE" "TRUE" ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Summary <center> <img src="creating_vec.PNG" width="60%"> </center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Creating seq using rep( ) ```r rep( c(0,0,7), times = 4) ``` -- ``` [1] 0 0 7 0 0 7 0 0 7 0 0 7 ``` -- ```r rep( c(2,4,2), each = 4) ``` -- ``` [1] 2 2 2 2 4 4 4 4 2 2 2 2 ``` -- ```r rep( c(0,7), times = c(4, 3) ) ``` -- ``` [1] 0 0 0 0 7 7 7 ``` -- ```r rep( 1:3, length.out = 9) ``` -- ``` [1] 1 2 3 1 2 3 1 2 3 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% #Accessing Vector Elements (1) Elements of a Vector can be accessed using indexing. The **[ ]** brackets are used for indexing. Indexing starts with position [1](1). Giving a negative value in the index drops that element from the result.[TRUE](TRUE), [FALSE](FALSE) can also be used for indexing. -- .pull-left[ ```r # Accessing vector elements # using position. t <- c("Sun","Mon","Tue") u <- t[c(2,3)] ``` ] -- .pull-right[ ```r # Accessing vector elements # using logical indexing. t <- c("Sun","Mon","Tue") v <- t[c(TRUE,FALSE,FALSE)] ``` ] -- ```r print(u) ``` ``` [1] "Mon" "Tue" ``` -- ```r print(v) ``` ``` [1] "Sun" ``` <!-- .footnote[[*] Not really. See next page.] --> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% #Accessing Vector Elements (2) Elements of a Vector can be accessed using indexing. The **[ ]** brackets are used for indexing. Indexing starts with position [1](1). Giving a negative value in the index drops that element from result.[TRUE](TRUE), [FALSE](FALSE) or 0 and 1 can also be used for indexing. -- .pull-left[ ```r # Accessing vector elements # using negative indexing. t <- c("Sun","Mon","Tue") x <- t[c(-2,-3)] ``` ] -- .pull-right[ ```r # Accessing vector elements # using indexing. t <- c("Sun","Mon","Tue") y <- t[c(4,8)] ``` ] -- ```r print(x) ``` ``` [1] "Sun" ``` -- ```r print(y) ``` ``` [1] NA NA ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% #Accessing Vector Elements (3) Elements of a Vector can be accessed using names. ```r I <- c("one"=1,"two"=2,"three"=3) ``` -- ```r print(I) ``` ``` one two three 1 2 3 ``` -- ```r *I["one"] ``` ``` one 1 ``` -- ```r *I[c("three","two")] ``` ``` three two 3 2 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Vector arithmetic Two vectors of same length can be added, subtracted, multiplied or divided giving the result as a vector output. ```r # Create two vectors. v1 <- c(3, 8, 4, 5, 0, 11) v2 <- c(4, 11, 0, 8, 1, 2) ``` -- .pull-left[ ```r # Vector addition. add.result <- v1+v2 # Vector subtraction. sub.result <- v1-v2 ``` ] -- .pull-right[ ```r # Vector multiplication. multi.result <- v1*v2 # Vector division. divi.result <- v1/v2 ``` ] --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Vector arithmetic When we execute the code, it produces the following result: ```r print(add.result) ``` ``` [1] 7 19 4 13 1 13 ``` -- ```r print(sub.result) ``` ``` [1] -1 -3 4 -3 -1 9 ``` -- ```r print(multi.result) ``` ``` [1] 12 88 0 40 0 22 ``` -- ```r print(divi.result) ``` ``` [1] 0.7500000 0.7272727 Inf 0.6250000 0.0000000 5.5000000 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% #Vector Element Recycling If we apply arithmetic operations to two vectors of unequal length, then the elements of the shorter vector are recycled to complete the operations. -- ```r v1 <- c(3,8,4,5,0,11) v2 <- c(4,11) # V2 becomes c(4,11,4,11,4,11) ``` -- ```r add.result <- v1+v2 print(add.result) ``` ``` [1] 7 19 8 16 4 22 ``` -- ```r sub.result <- v1-v2 print(sub.result) ``` ``` [1] -1 -3 0 -6 -4 0 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% #Vector Element Sorting Elements in a vector can be sorted using the sort() function. <center> <img src="images/sort.gif" width="100%"> </center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% #Vector Element Sorting Elements in a vector can be sorted using the sort() function. ```r v <- c(3,8,4,5,0,11, -9, 304) # Sort the elements of the vector. sort.result <- sort(v) ``` -- ```r # Sort the elements in the reverse order. revsort.result <- sort(v, decreasing = TRUE) ``` -- ```r # Sorting character vectors. v <- c("Red","Blue","yellow","violet") sort.result <- sort(v) ``` -- ```r # Sorting character vectors in reverse order. revsort.result <- sort(v, decreasing = TRUE) ``` --- class: middle background-image: url(images/matrix.PNG) background-size: 400px background-position: 90% 8% # R - Matrices Matrices are the `R` objects in which the elements are arranged in a two-dimensional rectangular layout. They contain elements of the same types. Though we can create a matrix containing only [characters](characters) or only [logical](logical) values, they are not of much use. We use matrices containing [numeric](numeric) elements. <center> A Matrix is created using the `matrix()` function. </center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Syntax The basic syntax for creating a matrix in R is: ```r matrix(data, nrow, ncol, byrow, dimnames) ``` Following is the description of the parameters used − 1. **[data](data)** is the input vector which becomes the data elements of the matrix. 1. **[nrow](nrow)** is the number of rows to be created. 1. **[ncol](ncol)** is the number of columns to be created. 1. **[byrow](byrow)** is a logical clue. If `TRUE` then the input vector elements are arranged by row. 1. **[dimname](dimname)** is the names assigned to the rows and columns. --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% ## Create a matrix taking a vector of numbers as input -- ```r # Elements are arranged sequentially by row. M <- matrix(c(3:14), nrow = 4, byrow = TRUE) print(M) ``` ``` [,1] [,2] [,3] [1,] 3 4 5 [2,] 6 7 8 [3,] 9 10 11 [4,] 12 13 14 ``` -- ```r # Elements are arranged sequentially by column. N <- matrix(c(3:14), nrow = 4, byrow = FALSE) print(N) ``` ``` [,1] [,2] [,3] [1,] 3 7 11 [2,] 4 8 12 [3,] 5 9 13 [4,] 6 10 14 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% #Example Create a matrix taking a vector of numbers as input. ```r # Define the column and row names. rownames = c("row1", "row2", "row3", "row4") colnames = c("col1", "col2", "col3") P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames)) print(P) ``` ``` col1 col2 col3 row1 3 4 5 row2 6 7 8 row3 9 10 11 row4 12 13 14 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% class: middle # How to extract elements from a matrix? As in the case of vectors, brackets [,] and inside, separated by a comma, the row number and column number that interest us are used to extract elements stored within a matrix. --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Example 1 - If we want to extract the value stored in row 4 and column 3. <center> <img src="images/matrixEx1.PNG" width="40%"> </center> -- ```r P[4,3] ``` ``` [1] 14 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Example 2 - If we want to retrieve the entire row 2 we use the following code. <center> <img src="images/matrixEx2.PNG" width="40%"> </center> -- ```r P[2,] ``` ``` col1 col2 col3 6 7 8 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Example 3 - If we want to retrieve the entire column 3 we use the following code. <center> <img src="images/matrixEx3.PNG" width="40%"> </center> -- ```r P[,3] ``` ``` row1 row2 row3 row4 5 8 11 14 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Example 4 If we want to show the original matrix without columns 2 we use the following code. <center> <img src="images/matrixEx4.PNG" width="40%"> </center> -- ```r P[,-2] ``` ``` col1 col3 row1 3 5 row2 6 8 row3 9 11 row4 12 14 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Example 5 If we want to show the original matrix without row 1 and column 3 we use the following code. <center> <img src="images/matrixEx5.PNG" width="40%"> </center> -- ```r P[-1,-3] ``` ``` col1 col2 row2 6 7 row3 9 10 row4 12 13 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% class: middle # Data frame The data frame is one of the most used objects since it allows grouping vectors with information of **different types** ([numerical](numerical), [alphanumeric](alphanumeric) or [logical](logical)) in the same object, the only restriction is that the vectors must have the **same length**. The `data.frame()` function is used to create a data frame. --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # data(iris) This famous (Fisher's or Anderson's) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica. -- <center> <img src="https://miro.medium.com/max/1275/1*7bnLKsChXq94QjtAiRn40w.png" width="100%"> </center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # data(iris) ```r data(iris) # ?iris ``` --
--- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% class: middle # Iris <center>
</center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% class: middle # Iris <center>
</center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # How to extract elements from a data frame? To retrieve the variables (columns) stored in a data frame, you can use the [$]($) operator, single square brackets [[]]([]) or double square brackets [[[]]]([[]]). Here are some examples to understand the differences between these options. -- - If we want to extract the ["Sepal.Length"]("Sepal.Length") variable from the iris data frame as a vector, we use the following code. -- ```r iris$Sepal.Length ``` -- ```r iris[,1] ``` -- ```r iris[["Sepal.Length"]] ``` -- ```r iris[,"Sepal.Length"] ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% #How to extract subsets of a data frame? The `subset(x, subset, select)` function can be used to extract parts of a data frame. The [x](x) parameter is used to indicate the original data frame, the [subset](subset) parameter is used to place the condition and the [select](select) parameter is used to keep only some of the variables in the data frame. -- * If we want the iris data frame only with the setosa species we use the following code. ```r subset(iris, subset=Species == "setosa") ``` ``` 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 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Logical comparison operators in R <center> <img src="https://bookdown.org/ndphillips/YaRrr/YaRrr_files/figure-html/comparison-1.png" alt="Comp" width="80%"> </center> --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Exercises * Con la base de datos Iris, seleccionar un subconjuto de los datos donde se cumplan las siguientes condiciones: * Solo seleccionar las flores de especie Versicolor * Solo seleccionar las flores de especie Versicolor o setosa * Las flores con Sepal.length mayor que 6 * Las flores con Petal.width menor o igual a 0.8 --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% class: middle # Creating Lists Lists are another type of object widely used to store objects of different types. The instruction to create a list is `list()`. Next we are going to create a list that contains [**three**](three) objects: * a vector with [**5**](5) random numbers called [**mivector**](mivector) * a 6 × 2 dimension matrix with the first twelve positive integers called [**matrix2**](matrix2) * and the third object will be the [**iris**](iris) data frame loaded in the previous slide. The instructions for creating the required list are shown below. -- ```r set.seed(1) mivector <- rnorm(n=5) matriz2 <- matrix(data=1:12, ncol=6) *milista <- list(E1=mivector, E2=matriz2, E3=iris) ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Creating Lists The `set.seed` function of line number 1 serves to set the seed in such a way that the random numbers generated in the second line with the `rnorm( )` function are always the same. ```r print(milista) ``` ``` $E1 [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 $E2 [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 3 5 7 9 11 [2,] 2 4 6 8 10 12 $E3 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 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% class: middle # How to extract items from a list? To retrieve items stored in a list, use the [$]($) operator, double square brackets [[[]]]([[]]) or single square brackets [[]]([]). Here are some examples to understand how to extract items from a list. --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # How to extract items from a list? ```r milista$E2 ``` ``` [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 3 5 7 9 11 [2,] 2 4 6 8 10 12 ``` -- ```r milista[[2]] ``` ``` [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 3 5 7 9 11 [2,] 2 4 6 8 10 12 ``` -- ```r milista[2] ``` ``` $E2 [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 3 5 7 9 11 [2,] 2 4 6 8 10 12 ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Using class for items in a list ```r class(milista$E2) ``` ``` [1] "matrix" ``` ```r class(milista[[2]]) ``` ``` [1] "matrix" ``` ```r class(milista[2]) ``` ``` [1] "list" ``` --- class: middle background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Exercises 1. Cargue la base de datos mtcars ([?mtcars](?mtcars)) 2. Identifique la estructura de la base de datos. 3. Realice un resumen general. 4. ¿Qué sucede con la variable vs? ¿Realmente es una variable númerica? 5. Realice un resumen para los autos con transmisión automática. 6. Guarde en una nueva base de datos los vehículos con más de 5 carburadores. 7. Almacene en un nuevo vector llamado temp, la variable mpg. 8. Seleccione de temp sólo los valores que superan los 20 mpg --- class: middle background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Database reading ## In what format to store a database? Preferably, save the information in `.csv` or `.txt` and then if we import this to R. --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Database reading (Excel) Se debe iniciar en la parte superior izquierda, no se deben dejar filas vacías, no se debe colorear, no se deben colocar bordes ni nada, se ingresa la información sin embellecer el contenido. Por último se guarda el archivo en la carpeta deseada y al momento de nombrar el archivo se debe modificar la opción tipo de archivo a csv (delimitado por comas). <center> <img src='images/excel.PNG' alt='RStudio' width='70%'> </center> --- class: middle background-image: url(logo.svg) background-size: 120px background-position: 10% 98% # Database reading ```r read.table(file, header = FALSE, sep = "", quote = "\"'", dec = ".", row.names, col.names) read.csv(file, header = TRUE, sep = ",", quote = "\"", dec = ".", fill = TRUE, comment.char = "", ...) read.csv2(file, header = TRUE, sep = ";", quote = "\"", dec = ",", fill = TRUE, comment.char = "", ...) read.delim(file, header = TRUE, sep = "\t", quote = "\"", dec = ".", fill = TRUE, comment.char = "", ...) read.delim2(file, header = TRUE, sep = "\t", quote = "\"", dec = ",", fill = TRUE, comment.char = "", ...) ``` --- background-image: url(logo.svg) background-size: 120px background-position: 10% 98% class: center, middle # Thanks! Slides created via the R package [**xaringan**](https://github.com/yihui/xaringan). [**https://apariciojohan.github.io/**](https://apariciojohan.github.io/) Johan Aparicio