class: center, middle, inverse, title-slide # Geography 13 ## Lecture 04: Data Type & Structure ### Mike Johnson --- ### If you are having trouble setting up R, RStudio, ect email me ASAP! --- # Computers (via R) convert bytes <-> hex <-> value <img src="lec-img/04-ascii-hex-binary.png"> --- # What's the difference between 3 (the number) and '3' (the character)? - `To us`: meaning - `To software`: hows its handled - `To a computer`: nothing -- ```r charToRaw('3') %>% rawToBits() ``` ``` [1] 01 01 00 00 01 01 00 00 ``` ```r writeBin(3, raw(), size = 8, endian = "big") ``` ``` [1] 40 08 00 00 00 00 00 00 ``` ```r 3 + 3 ``` ``` [1] 6 ``` ```r '3' + 3 ``` ``` Error in "3" + 3: non-numeric argument to binary operator ``` --- class: inverse, center, middle #
# Data Types --- # Data Types **Values** in R can be one of 6 different **types** : 1. numeric (e.g. `2`, `2.15`) 2. integer (e.g. `2L`) 3. character (e.g. `"x"`, `"Welcome!"`) 4. logical (e.g. `TRUE`, `FALSE`) 5. raw (e.g. holds `bytes`) 6. complex (e.g. `1+4i`) - _we are going to ignore_ - The `class` function tells us what kind of object is it (high-level) - The `typeof` function can tell us the object’s data type (low-level) --- ## 1. Numeric - Values with decimals -- - Of type "double" in computer science terms -- - Default computational data type in R. -- - Doubles can be specified in decimal (0.1234), scientific (1.23e4), or hexadecimal (0xcafe) form. -- - There are three special values unique to doubles: `Inf`, `-Inf`, and `NaN` (not a number). --- count: false .panel1-numerics-auto[ ```r *(x = 4.75) ``` ] .panel2-numerics-auto[ ``` [1] 4.75 ``` ] --- count: false .panel1-numerics-auto[ ```r (x = 4.75) *class(x) ``` ] .panel2-numerics-auto[ ``` [1] 4.75 ``` ``` [1] "numeric" ``` ] --- count: false .panel1-numerics-auto[ ```r (x = 4.75) class(x) *typeof(x) ``` ] .panel2-numerics-auto[ ``` [1] 4.75 ``` ``` [1] "numeric" ``` ``` [1] "double" ``` ] --- count: false .panel1-numerics-auto[ ```r (x = 4.75) class(x) typeof(x) *x * 2 ``` ] .panel2-numerics-auto[ ``` [1] 4.75 ``` ``` [1] "numeric" ``` ``` [1] "double" ``` ``` [1] 9.5 ``` ] --- count: false .panel1-numerics-auto[ ```r (x = 4.75) class(x) typeof(x) x * 2 *x + 2 ``` ] .panel2-numerics-auto[ ``` [1] 4.75 ``` ``` [1] "numeric" ``` ``` [1] "double" ``` ``` [1] 9.5 ``` ``` [1] 6.75 ``` ] --- count: false .panel1-numerics-auto[ ```r (x = 4.75) class(x) typeof(x) x * 2 x + 2 *x - 5 ``` ] .panel2-numerics-auto[ ``` [1] 4.75 ``` ``` [1] "numeric" ``` ``` [1] "double" ``` ``` [1] 9.5 ``` ``` [1] 6.75 ``` ``` [1] -0.25 ``` ] --- count: false .panel1-numerics-auto[ ```r (x = 4.75) class(x) typeof(x) x * 2 x + 2 x - 5 *x / 4 ``` ] .panel2-numerics-auto[ ``` [1] 4.75 ``` ``` [1] "numeric" ``` ``` [1] "double" ``` ``` [1] 9.5 ``` ``` [1] 6.75 ``` ``` [1] -0.25 ``` ``` [1] 1.1875 ``` ] --- count: false .panel1-numerics-auto[ ```r (x = 4.75) class(x) typeof(x) x * 2 x + 2 x - 5 x / 4 *x == 4 ``` ] .panel2-numerics-auto[ ``` [1] 4.75 ``` ``` [1] "numeric" ``` ``` [1] "double" ``` ``` [1] 9.5 ``` ``` [1] 6.75 ``` ``` [1] -0.25 ``` ``` [1] 1.1875 ``` ``` [1] FALSE ``` ] <style> .panel1-numerics-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-numerics-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-numerics-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ### 2. Integer - Values without decimals -- - To create an integer in R you must follow the a number with an uppercase L. -- - Take less memory then doubles but this is rarely an issue -- - One byte is **8** bits, - Each **bit** can represent two values (0,1), - One **byte** can hold 2^8^=256 values. - used for (0 to 255) --or-- (−128 to 127). --- count: false .panel1-integers-auto[ ```r *(x = 2) # defaults to numeric ``` ] .panel2-integers-auto[ ``` [1] 2 ``` ] --- count: false .panel1-integers-auto[ ```r (x = 2) # defaults to numeric *typeof(x) ``` ] .panel2-integers-auto[ ``` [1] 2 ``` ``` [1] "double" ``` ] --- count: false .panel1-integers-auto[ ```r (x = 2) # defaults to numeric typeof(x) *(y = 2L) # specifies integer ``` ] .panel2-integers-auto[ ``` [1] 2 ``` ``` [1] "double" ``` ``` [1] 2 ``` ] --- count: false .panel1-integers-auto[ ```r (x = 2) # defaults to numeric typeof(x) (y = 2L) # specifies integer *typeof(y) ``` ] .panel2-integers-auto[ ``` [1] 2 ``` ``` [1] "double" ``` ``` [1] 2 ``` ``` [1] "integer" ``` ] --- count: false .panel1-integers-auto[ ```r (x = 2) # defaults to numeric typeof(x) (y = 2L) # specifies integer typeof(y) *class(y) ``` ] .panel2-integers-auto[ ``` [1] 2 ``` ``` [1] "double" ``` ``` [1] 2 ``` ``` [1] "integer" ``` ``` [1] "integer" ``` ] --- count: false .panel1-integers-auto[ ```r (x = 2) # defaults to numeric typeof(x) (y = 2L) # specifies integer typeof(y) class(y) *(t = 1L / 5L) ``` ] .panel2-integers-auto[ ``` [1] 2 ``` ``` [1] "double" ``` ``` [1] 2 ``` ``` [1] "integer" ``` ``` [1] "integer" ``` ``` [1] 0.2 ``` ] --- count: false .panel1-integers-auto[ ```r (x = 2) # defaults to numeric typeof(x) (y = 2L) # specifies integer typeof(y) class(y) (t = 1L / 5L) *typeof(t) ``` ] .panel2-integers-auto[ ``` [1] 2 ``` ``` [1] "double" ``` ``` [1] 2 ``` ``` [1] "integer" ``` ``` [1] "integer" ``` ``` [1] 0.2 ``` ``` [1] "double" ``` ] --- count: false .panel1-integers-auto[ ```r (x = 2) # defaults to numeric typeof(x) (y = 2L) # specifies integer typeof(y) class(y) (t = 1L / 5L) typeof(t) ## Casting *as.integer(c(4.1, 5.2, 6.3, 6.4)) ``` ] .panel2-integers-auto[ ``` [1] 2 ``` ``` [1] "double" ``` ``` [1] 2 ``` ``` [1] "integer" ``` ``` [1] "integer" ``` ``` [1] 0.2 ``` ``` [1] "double" ``` ``` [1] 4 5 6 6 ``` ] <style> .panel1-integers-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-integers-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-integers-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ### 3. Character - character values stores text ranging in size from a single letter to a novel. -- - surrounded by `"` ("here") or `'` ('there'). -- - Special characters are escaped with `\`; see `?Quotes` for full details. --- count: false .panel1-character-auto[ ```r *(x = "a") ``` ] .panel2-character-auto[ ``` [1] "a" ``` ] --- count: false .panel1-character-auto[ ```r (x = "a") *(y = "bc") ``` ] .panel2-character-auto[ ``` [1] "a" ``` ``` [1] "bc" ``` ] --- count: false .panel1-character-auto[ ```r (x = "a") (y = "bc") *(z = "GIS is Great!!!") ``` ] .panel2-character-auto[ ``` [1] "a" ``` ``` [1] "bc" ``` ``` [1] "GIS is Great!!!" ``` ] <style> .panel1-character-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-character-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-character-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ### 4. Logical - Logical values store boolean values (`TRUE` and `FALSE`). -- - Usefull for checking conditions and controlling the flow of a program. -- - Or, for checking binary conditions (like on,off; open/closed; >100) -- - The idea of the T/F boolean will be one of the most important in this class -- - Logicals can be written in full (TRUE or FALSE), or abbreviated (T or F). --- count: false .panel1-logical-auto[ ```r *u = TRUE ``` ] .panel2-logical-auto[ ] --- count: false .panel1-logical-auto[ ```r u = TRUE *v = FALSE ``` ] .panel2-logical-auto[ ] --- count: false .panel1-logical-auto[ ```r u = TRUE v = FALSE *typeof(u) ``` ] .panel2-logical-auto[ ``` [1] "logical" ``` ] --- count: false .panel1-logical-auto[ ```r u = TRUE v = FALSE typeof(u) # if u AND v are TRUE *u & v ``` ] .panel2-logical-auto[ ``` [1] "logical" ``` ``` [1] FALSE ``` ] --- count: false .panel1-logical-auto[ ```r u = TRUE v = FALSE typeof(u) # if u AND v are TRUE u & v # if u OR v are TRUE *u | v ``` ] .panel2-logical-auto[ ``` [1] "logical" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ] --- count: false .panel1-logical-auto[ ```r u = TRUE v = FALSE typeof(u) # if u AND v are TRUE u & v # if u OR v are TRUE u | v # The opposite *!u ``` ] .panel2-logical-auto[ ``` [1] "logical" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ] --- count: false .panel1-logical-auto[ ```r u = TRUE v = FALSE typeof(u) # if u AND v are TRUE u & v # if u OR v are TRUE u | v # The opposite !u # Equality / conditions *2 == 6 ``` ] .panel2-logical-auto[ ``` [1] "logical" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ] --- count: false .panel1-logical-auto[ ```r u = TRUE v = FALSE typeof(u) # if u AND v are TRUE u & v # if u OR v are TRUE u | v # The opposite !u # Equality / conditions 2 == 6 *2 != 6 ``` ] .panel2-logical-auto[ ``` [1] "logical" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ] --- count: false .panel1-logical-auto[ ```r u = TRUE v = FALSE typeof(u) # if u AND v are TRUE u & v # if u OR v are TRUE u | v # The opposite !u # Equality / conditions 2 == 6 2 != 6 *"test" == "test" ``` ] .panel2-logical-auto[ ``` [1] "logical" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ] --- count: false .panel1-logical-auto[ ```r u = TRUE v = FALSE typeof(u) # if u AND v are TRUE u & v # if u OR v are TRUE u | v # The opposite !u # Equality / conditions 2 == 6 2 != 6 "test" == "test" *6 > 2 ``` ] .panel2-logical-auto[ ``` [1] "logical" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ] <style> .panel1-logical-auto { color: black; width: 48.5%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-logical-auto { color: black; width: 48.5%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-logical-auto { color: black; width: 0%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ### 5. Raw - The raw type is intended to hold raw bytes. - Useful to introduce, but will only be used at a conceptual level. --- count: false .panel1-raw-auto[ ```r *(x <- charToRaw("GIS is great!")) ``` ] .panel2-raw-auto[ ``` [1] 47 49 53 20 69 73 20 67 72 65 61 74 21 ``` ] --- count: false .panel1-raw-auto[ ```r (x <- charToRaw("GIS is great!")) *typeof(x) ``` ] .panel2-raw-auto[ ``` [1] 47 49 53 20 69 73 20 67 72 65 61 74 21 ``` ``` [1] "raw" ``` ] --- count: false .panel1-raw-auto[ ```r (x <- charToRaw("GIS is great!")) typeof(x) *rawToChar(x[1]) ``` ] .panel2-raw-auto[ ``` [1] 47 49 53 20 69 73 20 67 72 65 61 74 21 ``` ``` [1] "raw" ``` ``` [1] "G" ``` ] --- count: false .panel1-raw-auto[ ```r (x <- charToRaw("GIS is great!")) typeof(x) rawToChar(x[1]) *rawToChar(x) ``` ] .panel2-raw-auto[ ``` [1] 47 49 53 20 69 73 20 67 72 65 61 74 21 ``` ``` [1] "raw" ``` ``` [1] "G" ``` ``` [1] "GIS is great!" ``` ] --- count: false .panel1-raw-auto[ ```r (x <- charToRaw("GIS is great!")) typeof(x) rawToChar(x[1]) rawToChar(x) *rawToBits(x) ``` ] .panel2-raw-auto[ ``` [1] 47 49 53 20 69 73 20 67 72 65 61 74 21 ``` ``` [1] "raw" ``` ``` [1] "G" ``` ``` [1] "GIS is great!" ``` ``` [1] 01 01 01 00 00 00 01 00 01 00 00 01 00 00 01 00 01 01 00 00 01 00 01 00 00 00 00 00 00 01 00 00 01 00 00 01 00 01 01 00 01 01 00 00 01 01 01 00 00 00 00 00 00 [54] 01 00 00 01 01 01 00 00 01 01 00 00 01 00 00 01 01 01 00 01 00 01 00 00 01 01 00 01 00 00 00 00 01 01 00 00 00 01 00 01 01 01 00 01 00 00 00 00 01 00 00 ``` ] <style> .panel1-raw-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-raw-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-raw-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ### Bonus: Time Representing time is a somewhat complex problem. There are different calendars, hours, days, months, and leap years to consider. As a basic introduction, here is simple way to create date values. --- count: false .panel1-time-auto[ ```r *(start <- as.Date('2020-08-03')) ``` ] .panel2-time-auto[ ``` [1] "2020-08-03" ``` ] --- count: false .panel1-time-auto[ ```r (start <- as.Date('2020-08-03')) *(end <- as.Date('2020-09-11')) ``` ] .panel2-time-auto[ ``` [1] "2020-08-03" ``` ``` [1] "2020-09-11" ``` ] --- count: false .panel1-time-auto[ ```r (start <- as.Date('2020-08-03')) (end <- as.Date('2020-09-11')) *typeof(start) ``` ] .panel2-time-auto[ ``` [1] "2020-08-03" ``` ``` [1] "2020-09-11" ``` ``` [1] "double" ``` ] --- count: false .panel1-time-auto[ ```r (start <- as.Date('2020-08-03')) (end <- as.Date('2020-09-11')) typeof(start) *end - start ``` ] .panel2-time-auto[ ``` [1] "2020-08-03" ``` ``` [1] "2020-09-11" ``` ``` [1] "double" ``` ``` Time difference of 39 days ``` ] --- count: false .panel1-time-auto[ ```r (start <- as.Date('2020-08-03')) (end <- as.Date('2020-09-11')) typeof(start) end - start *format(start, "%m") ``` ] .panel2-time-auto[ ``` [1] "2020-08-03" ``` ``` [1] "2020-09-11" ``` ``` [1] "double" ``` ``` Time difference of 39 days ``` ``` [1] "08" ``` ] --- count: false .panel1-time-auto[ ```r (start <- as.Date('2020-08-03')) (end <- as.Date('2020-09-11')) typeof(start) end - start format(start, "%m") *format(start, "%y") ``` ] .panel2-time-auto[ ``` [1] "2020-08-03" ``` ``` [1] "2020-09-11" ``` ``` [1] "double" ``` ``` Time difference of 39 days ``` ``` [1] "08" ``` ``` [1] "20" ``` ] --- count: false .panel1-time-auto[ ```r (start <- as.Date('2020-08-03')) (end <- as.Date('2020-09-11')) typeof(start) end - start format(start, "%m") format(start, "%y") *format(start, "%Y") ``` ] .panel2-time-auto[ ``` [1] "2020-08-03" ``` ``` [1] "2020-09-11" ``` ``` [1] "double" ``` ``` Time difference of 39 days ``` ``` [1] "08" ``` ``` [1] "20" ``` ``` [1] "2020" ``` ] <style> .panel1-time-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-time-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-time-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- And there are more advanced classes as well that capture date and time. We will get into these latter in class. ```r as.POSIXlt(start) ``` ``` [1] "2020-08-03 UTC" ``` ```r as.POSIXct(start) ``` ``` [1] "2020-08-02 17:00:00 PDT" ``` --- class: inverse, center, middle #
# Data Structures ### Storing more then one value requires structure. --- - Vectors come in two types: **atomic** and **lists** -- - For atomic vectors, all elements must have the same **type**; -- - For lists, elements can have different types. -- - NULL serves as a generic zero length vector. -- - This diagram - taken from [here](https://adv-r.hadley.nz/vectors-chap.html) illustrates the basic relationships: <img src="lec-img/04-vec-list-01.png"> --- ### Atomic Vectors A vector containing one type of data is called an "atom" - Can created using the `c()` (combine) function. - The length can be checked with `length()` - There are four primary types of atomic vectors: logical, integer, double, and character (which contains strings). - Collectively integer and double vectors are known as numeric vectors. - Complex and raw atomic vectors are rare. <img src="lec-img/04-data-types.png" width="40%"> --- <img src="lec-img/04-vec-list-02.png"> --- count: false .panel1-atom-auto[ ```r # Numeric *(dbl_vec = c(1.9,2,3.5)) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) *typeof(dbl_vec) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) *length(dbl_vec) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) length(dbl_vec) *(int_vec = c(1L, 17L, 3L)) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ``` [1] 1 17 3 ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) length(dbl_vec) (int_vec = c(1L, 17L, 3L)) *typeof(int_vec) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ``` [1] 1 17 3 ``` ``` [1] "integer" ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) length(dbl_vec) (int_vec = c(1L, 17L, 3L)) typeof(int_vec) *length(int_vec) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ``` [1] 1 17 3 ``` ``` [1] "integer" ``` ``` [1] 3 ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) length(dbl_vec) (int_vec = c(1L, 17L, 3L)) typeof(int_vec) length(int_vec) *(lg_vec = c(TRUE, FALSE, F, T)) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ``` [1] 1 17 3 ``` ``` [1] "integer" ``` ``` [1] 3 ``` ``` [1] TRUE FALSE FALSE TRUE ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) length(dbl_vec) (int_vec = c(1L, 17L, 3L)) typeof(int_vec) length(int_vec) (lg_vec = c(TRUE, FALSE, F, T)) *typeof(lg_vec) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ``` [1] 1 17 3 ``` ``` [1] "integer" ``` ``` [1] 3 ``` ``` [1] TRUE FALSE FALSE TRUE ``` ``` [1] "logical" ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) length(dbl_vec) (int_vec = c(1L, 17L, 3L)) typeof(int_vec) length(int_vec) (lg_vec = c(TRUE, FALSE, F, T)) typeof(lg_vec) *length(lg_vec) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ``` [1] 1 17 3 ``` ``` [1] "integer" ``` ``` [1] 3 ``` ``` [1] TRUE FALSE FALSE TRUE ``` ``` [1] "logical" ``` ``` [1] 4 ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) length(dbl_vec) (int_vec = c(1L, 17L, 3L)) typeof(int_vec) length(int_vec) (lg_vec = c(TRUE, FALSE, F, T)) typeof(lg_vec) length(lg_vec) *(char_vec = c("GIS", "is", "Great!", "GIS is Great!")) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ``` [1] 1 17 3 ``` ``` [1] "integer" ``` ``` [1] 3 ``` ``` [1] TRUE FALSE FALSE TRUE ``` ``` [1] "logical" ``` ``` [1] 4 ``` ``` [1] "GIS" "is" "Great!" "GIS is Great!" ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) length(dbl_vec) (int_vec = c(1L, 17L, 3L)) typeof(int_vec) length(int_vec) (lg_vec = c(TRUE, FALSE, F, T)) typeof(lg_vec) length(lg_vec) (char_vec = c("GIS", "is", "Great!", "GIS is Great!")) *typeof(char_vec) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ``` [1] 1 17 3 ``` ``` [1] "integer" ``` ``` [1] 3 ``` ``` [1] TRUE FALSE FALSE TRUE ``` ``` [1] "logical" ``` ``` [1] 4 ``` ``` [1] "GIS" "is" "Great!" "GIS is Great!" ``` ``` [1] "character" ``` ] --- count: false .panel1-atom-auto[ ```r # Numeric (dbl_vec = c(1.9,2,3.5)) typeof(dbl_vec) length(dbl_vec) (int_vec = c(1L, 17L, 3L)) typeof(int_vec) length(int_vec) (lg_vec = c(TRUE, FALSE, F, T)) typeof(lg_vec) length(lg_vec) (char_vec = c("GIS", "is", "Great!", "GIS is Great!")) typeof(char_vec) *length(char_vec) ``` ] .panel2-atom-auto[ ``` [1] 1.9 2.0 3.5 ``` ``` [1] "double" ``` ``` [1] 3 ``` ``` [1] 1 17 3 ``` ``` [1] "integer" ``` ``` [1] 3 ``` ``` [1] TRUE FALSE FALSE TRUE ``` ``` [1] "logical" ``` ``` [1] 4 ``` ``` [1] "GIS" "is" "Great!" "GIS is Great!" ``` ``` [1] "character" ``` ``` [1] 4 ``` ] <style> .panel1-atom-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-atom-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-atom-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Missing Values! - Missing values still need a place holder - Missing values are denoted with `NA` (short for not applicable). - Missing values are 'infectious': most computations involving a missing value will return another missing value. --- count: false .panel1-na-auto[ ```r *(vec = c(5,6,7,8,NA)) ``` ] .panel2-na-auto[ ``` [1] 5 6 7 8 NA ``` ] --- count: false .panel1-na-auto[ ```r (vec = c(5,6,7,8,NA)) *mean(vec) ``` ] .panel2-na-auto[ ``` [1] 5 6 7 8 NA ``` ``` [1] NA ``` ] --- count: false .panel1-na-auto[ ```r (vec = c(5,6,7,8,NA)) mean(vec) *mean(vec, na.rm = TRUE) ``` ] .panel2-na-auto[ ``` [1] 5 6 7 8 NA ``` ``` [1] NA ``` ``` [1] 6.5 ``` ] --- count: false .panel1-na-auto[ ```r (vec = c(5,6,7,8,NA)) mean(vec) mean(vec, na.rm = TRUE) *x <- c(NA, 50, NA, 9) ``` ] .panel2-na-auto[ ``` [1] 5 6 7 8 NA ``` ``` [1] NA ``` ``` [1] 6.5 ``` ] --- count: false .panel1-na-auto[ ```r (vec = c(5,6,7,8,NA)) mean(vec) mean(vec, na.rm = TRUE) x <- c(NA, 50, NA, 9) *x == NA ``` ] .panel2-na-auto[ ``` [1] 5 6 7 8 NA ``` ``` [1] NA ``` ``` [1] 6.5 ``` ``` [1] NA NA NA NA ``` ] --- count: false .panel1-na-auto[ ```r (vec = c(5,6,7,8,NA)) mean(vec) mean(vec, na.rm = TRUE) x <- c(NA, 50, NA, 9) x == NA *is.na(x) ``` ] .panel2-na-auto[ ``` [1] 5 6 7 8 NA ``` ``` [1] NA ``` ``` [1] 6.5 ``` ``` [1] NA NA NA NA ``` ``` [1] TRUE FALSE TRUE FALSE ``` ] <style> .panel1-na-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-na-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-na-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Atoms must be of the same type! ## Cohersion - type is a property of the entire vector - When you try and combine different types they will be coerced in a fixed order: - character → double → integer → logical - Coercion often happens automatically. - You can deliberately coerce by using an `as.*()` function, like `as.logical()`, `as.integer()`, `as.double()`, or `as.character()`. - Failed coercion of strings generates a warning and a missing value --- count: false .panel1-atoms-auto[ ```r *c("a", 1) ``` ] .panel2-atoms-auto[ ``` [1] "a" "1" ``` ] --- count: false .panel1-atoms-auto[ ```r c("a", 1) *c("a", TRUE) ``` ] .panel2-atoms-auto[ ``` [1] "a" "1" ``` ``` [1] "a" "TRUE" ``` ] --- count: false .panel1-atoms-auto[ ```r c("a", 1) c("a", TRUE) *c(4.5, 1L) ``` ] .panel2-atoms-auto[ ``` [1] "a" "1" ``` ``` [1] "a" "TRUE" ``` ``` [1] 4.5 1.0 ``` ] --- count: false .panel1-atoms-auto[ ```r c("a", 1) c("a", TRUE) c(4.5, 1L) *c("1", 18, "GIS") ``` ] .panel2-atoms-auto[ ``` [1] "a" "1" ``` ``` [1] "a" "TRUE" ``` ``` [1] 4.5 1.0 ``` ``` [1] "1" "18" "GIS" ``` ] --- count: false .panel1-atoms-auto[ ```r c("a", 1) c("a", TRUE) c(4.5, 1L) c("1", 18, "GIS") *as.numeric(c("1", 18, "GIS")) ``` ] .panel2-atoms-auto[ ``` [1] "a" "1" ``` ``` [1] "a" "TRUE" ``` ``` [1] 4.5 1.0 ``` ``` [1] "1" "18" "GIS" ``` ``` [1] 1 18 NA ``` ] --- count: false .panel1-atoms-auto[ ```r c("a", 1) c("a", TRUE) c(4.5, 1L) c("1", 18, "GIS") as.numeric(c("1", 18, "GIS")) *as.logical(c("1", 18, "GIS")) ``` ] .panel2-atoms-auto[ ``` [1] "a" "1" ``` ``` [1] "a" "TRUE" ``` ``` [1] 4.5 1.0 ``` ``` [1] "1" "18" "GIS" ``` ``` [1] 1 18 NA ``` ``` [1] NA NA NA ``` ] <style> .panel1-atoms-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-atoms-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-atoms-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Names - In addition to naming the object, you can name elements making the "referancable" - names should be unique, and non-missing ```r (x <- c(a = 1, b = 2, c = 3)) ``` ``` a b c 1 2 3 ``` ```r # Using the attribute names() names(x) <- c("d", "e", "f") names(x) ``` ``` [1] "d" "e" "f" ``` ```r x ``` ``` d e f 1 2 3 ``` ```r # With the function setNames(): (x <- setNames(1:3, c("g", "h", "i"))) ``` ``` g h i 1 2 3 ``` --- # Diminsions - You probably noticed that the set of atomic vectors do not include a number of important structures like matrices (2D) or arrays (3D), factors, or date-times. - These types are built on top of atomic vectors by adding attributes. - Adding a `dim` attribute to a vector allows it to behave like a 2D matrix or a XD array. --- ### Matrix A matrix is also an 2D atom (row, column) - Same data types - Same column length --- count: false .panel1-matrix-auto[ ```r # Use matrix *(mat = matrix(1:9, nrow = 3)) ``` ] .panel2-matrix-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ] --- count: false .panel1-matrix-auto[ ```r # Use matrix (mat = matrix(1:9, nrow = 3)) # Use matrix *(mat2 = matrix(1:9, nrow = 3, byrow = TRUE)) ``` ] .panel2-matrix-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 ``` ] --- count: false .panel1-matrix-auto[ ```r # Use matrix (mat = matrix(1:9, nrow = 3)) # Use matrix (mat2 = matrix(1:9, nrow = 3, byrow = TRUE)) ## dim returns dimensions of an object *dim(mat2) ``` ] .panel2-matrix-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 ``` ``` [1] 3 3 ``` ] --- count: false .panel1-matrix-auto[ ```r # Use matrix (mat = matrix(1:9, nrow = 3)) # Use matrix (mat2 = matrix(1:9, nrow = 3, byrow = TRUE)) ## dim returns dimensions of an object dim(mat2) # set names using colnames *colnames(mat2) = c("A", "B", "C") ``` ] .panel2-matrix-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 ``` ``` [1] 3 3 ``` ] --- count: false .panel1-matrix-auto[ ```r # Use matrix (mat = matrix(1:9, nrow = 3)) # Use matrix (mat2 = matrix(1:9, nrow = 3, byrow = TRUE)) ## dim returns dimensions of an object dim(mat2) # set names using colnames colnames(mat2) = c("A", "B", "C") *mat2 ``` ] .panel2-matrix-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 ``` ``` [1] 3 3 ``` ``` A B C [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 ``` ] <style> .panel1-matrix-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-matrix-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-matrix-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ### Arrays An array is a 3D atom [row, column, slice] --- count: false .panel1-arrays-auto[ ```r *array(c(1:12), dim = c(3,2,2)) ``` ] .panel2-arrays-auto[ ``` , , 1 [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 , , 2 [,1] [,2] [1,] 7 10 [2,] 8 11 [3,] 9 12 ``` ] --- count: false .panel1-arrays-auto[ ```r array(c(1:12), dim = c(3,2,2)) *(arr = array(c(1:12), dim = c(2,3,2))) ``` ] .panel2-arrays-auto[ ``` , , 1 [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 , , 2 [,1] [,2] [1,] 7 10 [2,] 8 11 [3,] 9 12 ``` ``` , , 1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 , , 2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 ``` ] <style> .panel1-arrays-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-arrays-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-arrays-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- - A vector without a `dim` attribute set is often thought of as 1-dimensional, but actually has NULL dimensions. - Why? - Matrices with a single row or single column, or arrays with a single dimension are 1D. They may print similarly, but will behave differently. The differences aren’t too important, but it’s useful to know they exist in case you get strange output from a function (tapply() is a frequent offender). As always, use str() to reveal the differences. ```r str(c(1:3)) # 1d vector ``` ``` int [1:3] 1 2 3 ``` ```r str(matrix(1:3, ncol = 1)) # column vector ``` ``` int [1:3, 1] 1 2 3 ``` ```r str(matrix(1:3, nrow = 1)) # row vector ``` ``` int [1, 1:3] 1 2 3 ``` ```r str(array(1:3, 3)) # "array" vector ``` ``` int [1:3(1d)] 1 2 3 ``` --- # Lists Lists `extend` atomic vectors and allow each element to be any type. ```r (my_list <- list( matrix(1:4, nrow = 2), "GIS is great!", c(TRUE, FALSE, TRUE), c(2.3, 5.9) )) ``` ``` [[1]] [,1] [,2] [1,] 1 3 [2,] 2 4 [[2]] [1] "GIS is great!" [[3]] [1] TRUE FALSE TRUE [[4]] [1] 2.3 5.9 ``` ```r typeof(my_list) ``` ``` [1] "list" ``` --- # Lists can be recursive ```r (list_list = list(list("hi"))) ``` ``` [[1]] [[1]][[1]] [1] "hi" ``` ```r str(list_list) ``` ``` List of 1 $ :List of 1 ..$ : chr "hi" ``` --- ### Data Frames A `data.frame` is a data structure built on top of lists -- - a named _list of vectors_. -- - data.frames are one of the biggest and most important ideas in R, and one of the things that make R different from other programming languages -- - Unlike a regular list, in a `data.frame`, the length of each vector **must** be the same. -- - This gives data frames their rectangular structure and explains why they share the properties of both matrices and lists --- count: false # A small data.frame .panel1-smalldf-auto[ ```r *df1 <- data.frame(name = c("George", "Stan", "Carly"), * age = c(75,15,31), * retired = c(T,F,F)) ``` ] .panel2-smalldf-auto[ ] --- count: false # A small data.frame .panel1-smalldf-auto[ ```r df1 <- data.frame(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F)) *typeof(df1) ``` ] .panel2-smalldf-auto[ ``` [1] "list" ``` ] --- count: false # A small data.frame .panel1-smalldf-auto[ ```r df1 <- data.frame(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F)) typeof(df1) *attributes(df1) ``` ] .panel2-smalldf-auto[ ``` [1] "list" ``` ``` $names [1] "name" "age" "retired" $class [1] "data.frame" $row.names [1] 1 2 3 ``` ] --- count: false # A small data.frame .panel1-smalldf-auto[ ```r df1 <- data.frame(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F)) typeof(df1) attributes(df1) *str(df1) ``` ] .panel2-smalldf-auto[ ``` [1] "list" ``` ``` $names [1] "name" "age" "retired" $class [1] "data.frame" $row.names [1] 1 2 3 ``` ``` 'data.frame': 3 obs. of 3 variables: $ name : chr "George" "Stan" "Carly" $ age : num 75 15 31 $ retired: logi TRUE FALSE FALSE ``` ] <style> .panel1-smalldf-auto { color: black; width: 58.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-smalldf-auto { color: black; width: 38.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-smalldf-auto { color: black; width: 0%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-df-auto[ ```r *(num <- c(1,2,3,4)) ``` ] .panel2-df-auto[ ``` [1] 1 2 3 4 ``` ] --- count: false .panel1-df-auto[ ```r (num <- c(1,2,3,4)) *(color <- c("red", "white", * "green", NA)) ``` ] .panel2-df-auto[ ``` [1] 1 2 3 4 ``` ``` [1] "red" "white" "green" NA ``` ] --- count: false .panel1-df-auto[ ```r (num <- c(1,2,3,4)) (color <- c("red", "white", "green", NA)) *(boolean <- c(TRUE,TRUE, * TRUE,FALSE)) ``` ] .panel2-df-auto[ ``` [1] 1 2 3 4 ``` ``` [1] "red" "white" "green" NA ``` ``` [1] TRUE TRUE TRUE FALSE ``` ] --- count: false .panel1-df-auto[ ```r (num <- c(1,2,3,4)) (color <- c("red", "white", "green", NA)) (boolean <- c(TRUE,TRUE, TRUE,FALSE)) *(df = data.frame(num, color, boolean)) ``` ] .panel2-df-auto[ ``` [1] 1 2 3 4 ``` ``` [1] "red" "white" "green" NA ``` ``` [1] TRUE TRUE TRUE FALSE ``` ``` num color boolean 1 1 red TRUE 2 2 white TRUE 3 3 green TRUE 4 4 <NA> FALSE ``` ] <style> .panel1-df-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-df-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-df-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ### tibble A tibble (`tbl_df`), is a `data.frame` that enforces best practices: - Never changes the input type. - Can have columns that are lists. - Can have non-standard variable names. - Names can start with a number or contain spaces. - It only recycles vectors of length 1. - It never creates row names. --- count: false .panel1-tibble-auto[ ```r # data.frames can be converted to tibbles *(tib = as_tibble(df)) ``` ] .panel2-tibble-auto[ ``` # A tibble: 4 x 3 num color boolean <dbl> <chr> <lgl> 1 1 red TRUE 2 2 white TRUE 3 3 green TRUE 4 4 <NA> FALSE ``` ] --- count: false .panel1-tibble-auto[ ```r # data.frames can be converted to tibbles (tib = as_tibble(df)) *class(tib) ``` ] .panel2-tibble-auto[ ``` # A tibble: 4 x 3 num color boolean <dbl> <chr> <lgl> 1 1 red TRUE 2 2 white TRUE 3 3 green TRUE 4 4 <NA> FALSE ``` ``` [1] "tbl_df" "tbl" "data.frame" ``` ] --- count: false .panel1-tibble-auto[ ```r # data.frames can be converted to tibbles (tib = as_tibble(df)) class(tib) *typeof(tib) ``` ] .panel2-tibble-auto[ ``` # A tibble: 4 x 3 num color boolean <dbl> <chr> <lgl> 1 1 red TRUE 2 2 white TRUE 3 3 green TRUE 4 4 <NA> FALSE ``` ``` [1] "tbl_df" "tbl" "data.frame" ``` ``` [1] "list" ``` ] --- count: false .panel1-tibble-auto[ ```r # data.frames can be converted to tibbles (tib = as_tibble(df)) class(tib) typeof(tib) # can be converted back *data.frame(tib) ``` ] .panel2-tibble-auto[ ``` # A tibble: 4 x 3 num color boolean <dbl> <chr> <lgl> 1 1 red TRUE 2 2 white TRUE 3 3 green TRUE 4 4 <NA> FALSE ``` ``` [1] "tbl_df" "tbl" "data.frame" ``` ``` [1] "list" ``` ``` num color boolean 1 1 red TRUE 2 2 white TRUE 3 3 green TRUE 4 4 <NA> FALSE ``` ] <style> .panel1-tibble-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-tibble-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-tibble-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Subsetting - R’s subsetting operators are **fast** and powerful. - Subsetting in R is easy to learn but hard to master. - There are 3 subsetting operators, `[[`, `[`, and `$`. - Subsetting operators interact differently with different vector types (e.g., atomic vectors, lists, factors, matrices, and data frames). - Subsetting can be combined with assignment. --- count: false #Atomics .panel1-subvec-auto[ ```r *(x = c(3.4, 7, 18, 9.6)) ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ] --- count: false #Atomics .panel1-subvec-auto[ ```r (x = c(3.4, 7, 18, 9.6)) *x[3] ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ``` [1] 18 ``` ] --- count: false #Atomics .panel1-subvec-auto[ ```r (x = c(3.4, 7, 18, 9.6)) x[3] *x[c(3,4)] ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ``` [1] 18 ``` ``` [1] 18.0 9.6 ``` ] --- count: false #Atomics .panel1-subvec-auto[ ```r (x = c(3.4, 7, 18, 9.6)) x[3] x[c(3,4)] *x[-3] ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ``` [1] 18 ``` ``` [1] 18.0 9.6 ``` ``` [1] 3.4 7.0 9.6 ``` ] --- count: false #Atomics .panel1-subvec-auto[ ```r (x = c(3.4, 7, 18, 9.6)) x[3] x[c(3,4)] x[-3] *x[c(T,T,F,F)] ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ``` [1] 18 ``` ``` [1] 18.0 9.6 ``` ``` [1] 3.4 7.0 9.6 ``` ``` [1] 3.4 7.0 ``` ] --- count: false #Atomics .panel1-subvec-auto[ ```r (x = c(3.4, 7, 18, 9.6)) x[3] x[c(3,4)] x[-3] x[c(T,T,F,F)] *x = setNames(x, c('A', 'B','C','D')) ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ``` [1] 18 ``` ``` [1] 18.0 9.6 ``` ``` [1] 3.4 7.0 9.6 ``` ``` [1] 3.4 7.0 ``` ] --- count: false #Atomics .panel1-subvec-auto[ ```r (x = c(3.4, 7, 18, 9.6)) x[3] x[c(3,4)] x[-3] x[c(T,T,F,F)] x = setNames(x, c('A', 'B','C','D')) *x["A"] ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ``` [1] 18 ``` ``` [1] 18.0 9.6 ``` ``` [1] 3.4 7.0 9.6 ``` ``` [1] 3.4 7.0 ``` ``` A 3.4 ``` ] --- count: false #Atomics .panel1-subvec-auto[ ```r (x = c(3.4, 7, 18, 9.6)) x[3] x[c(3,4)] x[-3] x[c(T,T,F,F)] x = setNames(x, c('A', 'B','C','D')) x["A"] *x[c("A", "C")] ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ``` [1] 18 ``` ``` [1] 18.0 9.6 ``` ``` [1] 3.4 7.0 9.6 ``` ``` [1] 3.4 7.0 ``` ``` A 3.4 ``` ``` A C 3.4 18.0 ``` ] --- count: false #Atomics .panel1-subvec-auto[ ```r (x = c(3.4, 7, 18, 9.6)) x[3] x[c(3,4)] x[-3] x[c(T,T,F,F)] x = setNames(x, c('A', 'B','C','D')) x["A"] x[c("A", "C")] *x[c("A", "A")] ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ``` [1] 18 ``` ``` [1] 18.0 9.6 ``` ``` [1] 3.4 7.0 9.6 ``` ``` [1] 3.4 7.0 ``` ``` A 3.4 ``` ``` A C 3.4 18.0 ``` ``` A A 3.4 3.4 ``` ] --- count: false #Atomics .panel1-subvec-auto[ ```r (x = c(3.4, 7, 18, 9.6)) x[3] x[c(3,4)] x[-3] x[c(T,T,F,F)] x = setNames(x, c('A', 'B','C','D')) x["A"] x[c("A", "C")] x[c("A", "A")] ``` ] .panel2-subvec-auto[ ``` [1] 3.4 7.0 18.0 9.6 ``` ``` [1] 18 ``` ``` [1] 18.0 9.6 ``` ``` [1] 3.4 7.0 9.6 ``` ``` [1] 3.4 7.0 ``` ``` A 3.4 ``` ``` A C 3.4 18.0 ``` ``` A A 3.4 3.4 ``` ] <style> .panel1-subvec-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-subvec-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-subvec-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false #Matrices .panel1-submat-auto[ ```r *(x = matrix(1:9, nrow = 3)) ``` ] .panel2-submat-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ] --- count: false #Matrices .panel1-submat-auto[ ```r (x = matrix(1:9, nrow = 3)) *x[3,] ``` ] .panel2-submat-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ``` [1] 3 6 9 ``` ] --- count: false #Matrices .panel1-submat-auto[ ```r (x = matrix(1:9, nrow = 3)) x[3,] *x[,3] ``` ] .panel2-submat-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ``` [1] 3 6 9 ``` ``` [1] 7 8 9 ``` ] --- count: false #Matrices .panel1-submat-auto[ ```r (x = matrix(1:9, nrow = 3)) x[3,] x[,3] *x[3,3] ``` ] .panel2-submat-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ``` [1] 3 6 9 ``` ``` [1] 7 8 9 ``` ``` [1] 9 ``` ] --- count: false #Matrices .panel1-submat-auto[ ```r (x = matrix(1:9, nrow = 3)) x[3,] x[,3] x[3,3] *x[1:2,1:2] ``` ] .panel2-submat-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ``` [1] 3 6 9 ``` ``` [1] 7 8 9 ``` ``` [1] 9 ``` ``` [,1] [,2] [1,] 1 4 [2,] 2 5 ``` ] --- count: false #Matrices .panel1-submat-auto[ ```r (x = matrix(1:9, nrow = 3)) x[3,] x[,3] x[3,3] x[1:2,1:2] *x[-1,] ``` ] .panel2-submat-auto[ ``` [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 ``` ``` [1] 3 6 9 ``` ``` [1] 7 8 9 ``` ``` [1] 9 ``` ``` [,1] [,2] [1,] 1 4 [2,] 2 5 ``` ``` [,1] [,2] [,3] [1,] 2 5 8 [2,] 3 6 9 ``` ] <style> .panel1-submat-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-submat-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-submat-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false #Arrays .panel1-subarr-auto[ ```r *(x = array(1:12, dim = c(2,2,3))) ``` ] .panel2-subarr-auto[ ``` , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12 ``` ] --- count: false #Arrays .panel1-subarr-auto[ ```r (x = array(1:12, dim = c(2,2,3))) *x[1,,] ``` ] .panel2-subarr-auto[ ``` , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12 ``` ``` [,1] [,2] [,3] [1,] 1 5 9 [2,] 3 7 11 ``` ] --- count: false #Arrays .panel1-subarr-auto[ ```r (x = array(1:12, dim = c(2,2,3))) x[1,,] *x[,1,] ``` ] .panel2-subarr-auto[ ``` , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12 ``` ``` [,1] [,2] [,3] [1,] 1 5 9 [2,] 3 7 11 ``` ``` [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 ``` ] --- count: false #Arrays .panel1-subarr-auto[ ```r (x = array(1:12, dim = c(2,2,3))) x[1,,] x[,1,] *x[,,1] ``` ] .panel2-subarr-auto[ ``` , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12 ``` ``` [,1] [,2] [,3] [1,] 1 5 9 [2,] 3 7 11 ``` ``` [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 ``` ``` [,1] [,2] [1,] 1 3 [2,] 2 4 ``` ] --- count: false #Arrays .panel1-subarr-auto[ ```r (x = array(1:12, dim = c(2,2,3))) x[1,,] x[,1,] x[,,1] ``` ] .panel2-subarr-auto[ ``` , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12 ``` ``` [,1] [,2] [,3] [1,] 1 5 9 [2,] 3 7 11 ``` ``` [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 ``` ``` [,1] [,2] [1,] 1 3 [2,] 2 4 ``` ] <style> .panel1-subarr-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-subarr-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-subarr-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false #Lists .panel1-sublist-auto[ ```r *(ll <- list(name = c("George", "Stan", "Carly"), * age = c(75,15,31), * retired = c(T,F,F))) ``` ] .panel2-sublist-auto[ ``` $name [1] "George" "Stan" "Carly" $age [1] 75 15 31 $retired [1] TRUE FALSE FALSE ``` ] --- count: false #Lists .panel1-sublist-auto[ ```r (ll <- list(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) *ll$name ``` ] .panel2-sublist-auto[ ``` $name [1] "George" "Stan" "Carly" $age [1] 75 15 31 $retired [1] TRUE FALSE FALSE ``` ``` [1] "George" "Stan" "Carly" ``` ] --- count: false #Lists .panel1-sublist-auto[ ```r (ll <- list(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) ll$name *ll$name[1] ``` ] .panel2-sublist-auto[ ``` $name [1] "George" "Stan" "Carly" $age [1] 75 15 31 $retired [1] TRUE FALSE FALSE ``` ``` [1] "George" "Stan" "Carly" ``` ``` [1] "George" ``` ] --- count: false #Lists .panel1-sublist-auto[ ```r (ll <- list(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) ll$name ll$name[1] *ll[[1]] ``` ] .panel2-sublist-auto[ ``` $name [1] "George" "Stan" "Carly" $age [1] 75 15 31 $retired [1] TRUE FALSE FALSE ``` ``` [1] "George" "Stan" "Carly" ``` ``` [1] "George" ``` ``` [1] "George" "Stan" "Carly" ``` ] --- count: false #Lists .panel1-sublist-auto[ ```r (ll <- list(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) ll$name ll$name[1] ll[[1]] *ll[[1]][1] ``` ] .panel2-sublist-auto[ ``` $name [1] "George" "Stan" "Carly" $age [1] 75 15 31 $retired [1] TRUE FALSE FALSE ``` ``` [1] "George" "Stan" "Carly" ``` ``` [1] "George" ``` ``` [1] "George" "Stan" "Carly" ``` ``` [1] "George" ``` ] --- count: false #Lists .panel1-sublist-auto[ ```r (ll <- list(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) ll$name ll$name[1] ll[[1]] ll[[1]][1] *ll[['name']][1] ``` ] .panel2-sublist-auto[ ``` $name [1] "George" "Stan" "Carly" $age [1] 75 15 31 $retired [1] TRUE FALSE FALSE ``` ``` [1] "George" "Stan" "Carly" ``` ``` [1] "George" ``` ``` [1] "George" "Stan" "Carly" ``` ``` [1] "George" ``` ``` [1] "George" ``` ] <style> .panel1-sublist-auto { color: black; width: 58.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-sublist-auto { color: black; width: 38.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-sublist-auto { color: black; width: 0%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Lists are not Matrices ```r # The name "Stan" ll[1,2] ``` ``` Error in ll[1, 2]: incorrect number of dimensions ``` ```r # Stans Information ll[2,] ``` ``` Error in ll[2, ]: incorrect number of dimensions ``` --- count: false #Enter data.frames .panel1-subdf-auto[ ```r *(df <- data.frame(name = c("George", "Stan", "Carly"), * age = c(75,15,31), * retired = c(T,F,F))) ``` ] .panel2-subdf-auto[ ``` name age retired 1 George 75 TRUE 2 Stan 15 FALSE 3 Carly 31 FALSE ``` ] --- count: false #Enter data.frames .panel1-subdf-auto[ ```r (df <- data.frame(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) # Like a Matrix! *df[1,2] ``` ] .panel2-subdf-auto[ ``` name age retired 1 George 75 TRUE 2 Stan 15 FALSE 3 Carly 31 FALSE ``` ``` [1] 75 ``` ] --- count: false #Enter data.frames .panel1-subdf-auto[ ```r (df <- data.frame(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) # Like a Matrix! df[1,2] *df[2,] ``` ] .panel2-subdf-auto[ ``` name age retired 1 George 75 TRUE 2 Stan 15 FALSE 3 Carly 31 FALSE ``` ``` [1] 75 ``` ``` name age retired 2 Stan 15 FALSE ``` ] --- count: false #Enter data.frames .panel1-subdf-auto[ ```r (df <- data.frame(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) # Like a Matrix! df[1,2] df[2,] # Like a list! *df[[1]][1] ``` ] .panel2-subdf-auto[ ``` name age retired 1 George 75 TRUE 2 Stan 15 FALSE 3 Carly 31 FALSE ``` ``` [1] 75 ``` ``` name age retired 2 Stan 15 FALSE ``` ``` [1] "George" ``` ] --- count: false #Enter data.frames .panel1-subdf-auto[ ```r (df <- data.frame(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) # Like a Matrix! df[1,2] df[2,] # Like a list! df[[1]][1] *df$name[1] ``` ] .panel2-subdf-auto[ ``` name age retired 1 George 75 TRUE 2 Stan 15 FALSE 3 Carly 31 FALSE ``` ``` [1] 75 ``` ``` name age retired 2 Stan 15 FALSE ``` ``` [1] "George" ``` ``` [1] "George" ``` ] --- count: false #Enter data.frames .panel1-subdf-auto[ ```r (df <- data.frame(name = c("George", "Stan", "Carly"), age = c(75,15,31), retired = c(T,F,F))) # Like a Matrix! df[1,2] df[2,] # Like a list! df[[1]][1] df$name[1] ``` ] .panel2-subdf-auto[ ``` name age retired 1 George 75 TRUE 2 Stan 15 FALSE 3 Carly 31 FALSE ``` ``` [1] 75 ``` ``` name age retired 2 Stan 15 FALSE ``` ``` [1] "George" ``` ``` [1] "George" ``` ] <style> .panel1-subdf-auto { color: black; width: 58.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-subdf-auto { color: black; width: 38.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-subdf-auto { color: black; width: 0%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: center, middle, inverse # Type and Structure in Practice ### Real data examples --- ### Storm Dataset - Many packages come with loaded datasets. - `dplyr` (part of the tidyverse) has a `storms` dataset <img src="lecture-04_files/figure-html/unnamed-chunk-8-1.png" width="432" /> --- count: false .panel1-storms-auto[ ```r #preview dataset *head(storms,3) ``` ] .panel2-storms-auto[ ``` # A tibble: 3 x 13 name year month day hour lat long status category wind pressure ts_diameter hu_diameter <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr> <ord> <int> <int> <dbl> <dbl> 1 Amy 1975 6 27 0 27.5 -79 tropical depression -1 25 1013 NA NA 2 Amy 1975 6 27 6 28.5 -79 tropical depression -1 25 1013 NA NA 3 Amy 1975 6 27 12 29.5 -79 tropical depression -1 25 1013 NA NA ``` ] --- count: false .panel1-storms-auto[ ```r #preview dataset head(storms,3) # Get data diminsions *dim(storms) ``` ] .panel2-storms-auto[ ``` # A tibble: 3 x 13 name year month day hour lat long status category wind pressure ts_diameter hu_diameter <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr> <ord> <int> <int> <dbl> <dbl> 1 Amy 1975 6 27 0 27.5 -79 tropical depression -1 25 1013 NA NA 2 Amy 1975 6 27 6 28.5 -79 tropical depression -1 25 1013 NA NA 3 Amy 1975 6 27 12 29.5 -79 tropical depression -1 25 1013 NA NA ``` ``` [1] 10010 13 ``` ] --- count: false .panel1-storms-auto[ ```r #preview dataset head(storms,3) # Get data diminsions dim(storms) *str(storms) ``` ] .panel2-storms-auto[ ``` # A tibble: 3 x 13 name year month day hour lat long status category wind pressure ts_diameter hu_diameter <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr> <ord> <int> <int> <dbl> <dbl> 1 Amy 1975 6 27 0 27.5 -79 tropical depression -1 25 1013 NA NA 2 Amy 1975 6 27 6 28.5 -79 tropical depression -1 25 1013 NA NA 3 Amy 1975 6 27 12 29.5 -79 tropical depression -1 25 1013 NA NA ``` ``` [1] 10010 13 ``` ``` tibble [10,010 × 13] (S3: tbl_df/tbl/data.frame) $ name : chr [1:10010] "Amy" "Amy" "Amy" "Amy" ... $ year : num [1:10010] 1975 1975 1975 1975 1975 ... $ month : num [1:10010] 6 6 6 6 6 6 6 6 6 6 ... $ day : int [1:10010] 27 27 27 27 28 28 28 28 29 29 ... $ hour : num [1:10010] 0 6 12 18 0 6 12 18 0 6 ... $ lat : num [1:10010] 27.5 28.5 29.5 30.5 31.5 32.4 33.3 34 34.4 34 ... $ long : num [1:10010] -79 -79 -79 -79 -78.8 -78.7 -78 -77 -75.8 -74.8 ... $ status : chr [1:10010] "tropical depression" "tropical depression" "tropical depression" "tropical depression" ... $ category : Ord.factor w/ 7 levels "-1"<"0"<"1"<"2"<..: 1 1 1 1 1 1 1 1 2 2 ... $ wind : int [1:10010] 25 25 25 25 25 25 25 30 35 40 ... $ pressure : int [1:10010] 1013 1013 1013 1013 1012 1012 1011 1006 1004 1002 ... $ ts_diameter: num [1:10010] NA NA NA NA NA NA NA NA NA NA ... $ hu_diameter: num [1:10010] NA NA NA NA NA NA NA NA NA NA ... ``` ] --- count: false .panel1-storms-auto[ ```r #preview dataset head(storms,3) # Get data diminsions dim(storms) str(storms) *storms$name[1:5] ``` ] .panel2-storms-auto[ ``` # A tibble: 3 x 13 name year month day hour lat long status category wind pressure ts_diameter hu_diameter <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr> <ord> <int> <int> <dbl> <dbl> 1 Amy 1975 6 27 0 27.5 -79 tropical depression -1 25 1013 NA NA 2 Amy 1975 6 27 6 28.5 -79 tropical depression -1 25 1013 NA NA 3 Amy 1975 6 27 12 29.5 -79 tropical depression -1 25 1013 NA NA ``` ``` [1] 10010 13 ``` ``` tibble [10,010 × 13] (S3: tbl_df/tbl/data.frame) $ name : chr [1:10010] "Amy" "Amy" "Amy" "Amy" ... $ year : num [1:10010] 1975 1975 1975 1975 1975 ... $ month : num [1:10010] 6 6 6 6 6 6 6 6 6 6 ... $ day : int [1:10010] 27 27 27 27 28 28 28 28 29 29 ... $ hour : num [1:10010] 0 6 12 18 0 6 12 18 0 6 ... $ lat : num [1:10010] 27.5 28.5 29.5 30.5 31.5 32.4 33.3 34 34.4 34 ... $ long : num [1:10010] -79 -79 -79 -79 -78.8 -78.7 -78 -77 -75.8 -74.8 ... $ status : chr [1:10010] "tropical depression" "tropical depression" "tropical depression" "tropical depression" ... $ category : Ord.factor w/ 7 levels "-1"<"0"<"1"<"2"<..: 1 1 1 1 1 1 1 1 2 2 ... $ wind : int [1:10010] 25 25 25 25 25 25 25 30 35 40 ... $ pressure : int [1:10010] 1013 1013 1013 1013 1012 1012 1011 1006 1004 1002 ... $ ts_diameter: num [1:10010] NA NA NA NA NA NA NA NA NA NA ... $ hu_diameter: num [1:10010] NA NA NA NA NA NA NA NA NA NA ... ``` ``` [1] "Amy" "Amy" "Amy" "Amy" "Amy" ``` ] --- count: false .panel1-storms-auto[ ```r #preview dataset head(storms,3) # Get data diminsions dim(storms) str(storms) storms$name[1:5] *storms[[1]][1:5] ``` ] .panel2-storms-auto[ ``` # A tibble: 3 x 13 name year month day hour lat long status category wind pressure ts_diameter hu_diameter <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr> <ord> <int> <int> <dbl> <dbl> 1 Amy 1975 6 27 0 27.5 -79 tropical depression -1 25 1013 NA NA 2 Amy 1975 6 27 6 28.5 -79 tropical depression -1 25 1013 NA NA 3 Amy 1975 6 27 12 29.5 -79 tropical depression -1 25 1013 NA NA ``` ``` [1] 10010 13 ``` ``` tibble [10,010 × 13] (S3: tbl_df/tbl/data.frame) $ name : chr [1:10010] "Amy" "Amy" "Amy" "Amy" ... $ year : num [1:10010] 1975 1975 1975 1975 1975 ... $ month : num [1:10010] 6 6 6 6 6 6 6 6 6 6 ... $ day : int [1:10010] 27 27 27 27 28 28 28 28 29 29 ... $ hour : num [1:10010] 0 6 12 18 0 6 12 18 0 6 ... $ lat : num [1:10010] 27.5 28.5 29.5 30.5 31.5 32.4 33.3 34 34.4 34 ... $ long : num [1:10010] -79 -79 -79 -79 -78.8 -78.7 -78 -77 -75.8 -74.8 ... $ status : chr [1:10010] "tropical depression" "tropical depression" "tropical depression" "tropical depression" ... $ category : Ord.factor w/ 7 levels "-1"<"0"<"1"<"2"<..: 1 1 1 1 1 1 1 1 2 2 ... $ wind : int [1:10010] 25 25 25 25 25 25 25 30 35 40 ... $ pressure : int [1:10010] 1013 1013 1013 1013 1012 1012 1011 1006 1004 1002 ... $ ts_diameter: num [1:10010] NA NA NA NA NA NA NA NA NA NA ... $ hu_diameter: num [1:10010] NA NA NA NA NA NA NA NA NA NA ... ``` ``` [1] "Amy" "Amy" "Amy" "Amy" "Amy" ``` ``` [1] "Amy" "Amy" "Amy" "Amy" "Amy" ``` ] --- count: false .panel1-storms-auto[ ```r #preview dataset head(storms,3) # Get data diminsions dim(storms) str(storms) storms$name[1:5] storms[[1]][1:5] *storms[1:5,1:5] ``` ] .panel2-storms-auto[ ``` # A tibble: 3 x 13 name year month day hour lat long status category wind pressure ts_diameter hu_diameter <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr> <ord> <int> <int> <dbl> <dbl> 1 Amy 1975 6 27 0 27.5 -79 tropical depression -1 25 1013 NA NA 2 Amy 1975 6 27 6 28.5 -79 tropical depression -1 25 1013 NA NA 3 Amy 1975 6 27 12 29.5 -79 tropical depression -1 25 1013 NA NA ``` ``` [1] 10010 13 ``` ``` tibble [10,010 × 13] (S3: tbl_df/tbl/data.frame) $ name : chr [1:10010] "Amy" "Amy" "Amy" "Amy" ... $ year : num [1:10010] 1975 1975 1975 1975 1975 ... $ month : num [1:10010] 6 6 6 6 6 6 6 6 6 6 ... $ day : int [1:10010] 27 27 27 27 28 28 28 28 29 29 ... $ hour : num [1:10010] 0 6 12 18 0 6 12 18 0 6 ... $ lat : num [1:10010] 27.5 28.5 29.5 30.5 31.5 32.4 33.3 34 34.4 34 ... $ long : num [1:10010] -79 -79 -79 -79 -78.8 -78.7 -78 -77 -75.8 -74.8 ... $ status : chr [1:10010] "tropical depression" "tropical depression" "tropical depression" "tropical depression" ... $ category : Ord.factor w/ 7 levels "-1"<"0"<"1"<"2"<..: 1 1 1 1 1 1 1 1 2 2 ... $ wind : int [1:10010] 25 25 25 25 25 25 25 30 35 40 ... $ pressure : int [1:10010] 1013 1013 1013 1013 1012 1012 1011 1006 1004 1002 ... $ ts_diameter: num [1:10010] NA NA NA NA NA NA NA NA NA NA ... $ hu_diameter: num [1:10010] NA NA NA NA NA NA NA NA NA NA ... ``` ``` [1] "Amy" "Amy" "Amy" "Amy" "Amy" ``` ``` [1] "Amy" "Amy" "Amy" "Amy" "Amy" ``` ``` # A tibble: 5 x 5 name year month day hour <chr> <dbl> <dbl> <int> <dbl> 1 Amy 1975 6 27 0 2 Amy 1975 6 27 6 3 Amy 1975 6 27 12 4 Amy 1975 6 27 18 5 Amy 1975 6 28 0 ``` ] <style> .panel1-storms-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-storms-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-storms-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false #Hurricane Ana .panel1-ana-auto[ ```r *(ana = storms[storms$name == "Ana",]) ``` ] .panel2-ana-auto[ ``` # A tibble: 100 x 13 name year month day hour lat long status category wind pressure ts_diameter hu_diameter <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr> <ord> <int> <int> <dbl> <dbl> 1 Ana 1979 6 19 12 10 -45 tropical depression -1 25 1011 NA NA 2 Ana 1979 6 19 18 10.2 -46 tropical depression -1 25 1011 NA NA 3 Ana 1979 6 20 0 10.5 -47 tropical depression -1 25 1010 NA NA 4 Ana 1979 6 20 6 10.9 -48.1 tropical depression -1 25 1010 NA NA 5 Ana 1979 6 20 12 11.3 -49.2 tropical depression -1 25 1010 NA NA 6 Ana 1979 6 20 18 11.8 -50.2 tropical depression -1 25 1009 NA NA 7 Ana 1979 6 21 0 12.3 -51.1 tropical depression -1 30 1009 NA NA 8 Ana 1979 6 21 6 12.9 -51.9 tropical depression -1 30 1008 NA NA 9 Ana 1979 6 21 12 13.5 -52.7 tropical depression -1 30 1008 NA NA 10 Ana 1979 6 21 18 13.9 -53.5 tropical depression -1 30 1007 NA NA # … with 90 more rows ``` ] --- count: false #Hurricane Ana .panel1-ana-auto[ ```r (ana = storms[storms$name == "Ana",]) *unique(ana$year) ``` ] .panel2-ana-auto[ ``` # A tibble: 100 x 13 name year month day hour lat long status category wind pressure ts_diameter hu_diameter <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr> <ord> <int> <int> <dbl> <dbl> 1 Ana 1979 6 19 12 10 -45 tropical depression -1 25 1011 NA NA 2 Ana 1979 6 19 18 10.2 -46 tropical depression -1 25 1011 NA NA 3 Ana 1979 6 20 0 10.5 -47 tropical depression -1 25 1010 NA NA 4 Ana 1979 6 20 6 10.9 -48.1 tropical depression -1 25 1010 NA NA 5 Ana 1979 6 20 12 11.3 -49.2 tropical depression -1 25 1010 NA NA 6 Ana 1979 6 20 18 11.8 -50.2 tropical depression -1 25 1009 NA NA 7 Ana 1979 6 21 0 12.3 -51.1 tropical depression -1 30 1009 NA NA 8 Ana 1979 6 21 6 12.9 -51.9 tropical depression -1 30 1008 NA NA 9 Ana 1979 6 21 12 13.5 -52.7 tropical depression -1 30 1008 NA NA 10 Ana 1979 6 21 18 13.9 -53.5 tropical depression -1 30 1007 NA NA # … with 90 more rows ``` ``` [1] 1979 1985 1991 1997 2003 2009 2015 ``` ] <style> .panel1-ana-auto { color: black; width: 38.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ana-auto { color: black; width: 58.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ana-auto { color: black; width: 0%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false #Hurricane Ana .panel1-ana2-auto[ ```r *ana_2009 = ana[ana$year == 2009,] ``` ] .panel2-ana2-auto[ ] --- count: false #Hurricane Ana .panel1-ana2-auto[ ```r ana_2009 = ana[ana$year == 2009,] *{plot(ana_2009$long, ana_2009$lat, * col = ana_2009$day, pch = 16, cex = 2) *lines(ana_2009$long, ana_2009$lat)} ``` ] .panel2-ana2-auto[ <img src="lecture-04_files/figure-html/ana2_auto_02_output-1.png" width="432" /> ] <style> .panel1-ana2-auto { color: black; width: 38.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ana2-auto { color: black; width: 58.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ana2-auto { color: black; width: 0%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> count: false .panel1-raster-auto[ ```r *matrix(1:100, nrow = 10) ``` ] .panel2-raster-auto[ ``` [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100 ``` ] --- count: false .panel1-raster-auto[ ```r matrix(1:100, nrow = 10) %>% * raster() ``` ] .panel2-raster-auto[ ``` class : RasterLayer dimensions : 10, 10, 100 (nrow, ncol, ncell) resolution : 0.1, 0.1 (x, y) extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax) crs : NA source : memory names : layer values : 1, 100 (min, max) ``` ] --- count: false .panel1-raster-auto[ ```r matrix(1:100, nrow = 10) %>% raster() %>% * rasterVis::levelplot() ``` ] .panel2-raster-auto[ <img src="lecture-04_files/figure-html/raster_auto_03_output-1.png" width="432" /> ] <style> .panel1-raster-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-raster-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-raster-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-raster2-auto[ ```r *array(1:300, dim = c(10,10,3)) ``` ] .panel2-raster2-auto[ ``` , , 1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100 , , 2 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 101 111 121 131 141 151 161 171 181 191 [2,] 102 112 122 132 142 152 162 172 182 192 [3,] 103 113 123 133 143 153 163 173 183 193 [4,] 104 114 124 134 144 154 164 174 184 194 [5,] 105 115 125 135 145 155 165 175 185 195 [6,] 106 116 126 136 146 156 166 176 186 196 [7,] 107 117 127 137 147 157 167 177 187 197 [8,] 108 118 128 138 148 158 168 178 188 198 [9,] 109 119 129 139 149 159 169 179 189 199 [10,] 110 120 130 140 150 160 170 180 190 200 , , 3 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 201 211 221 231 241 251 261 271 281 291 [2,] 202 212 222 232 242 252 262 272 282 292 [3,] 203 213 223 233 243 253 263 273 283 293 [4,] 204 214 224 234 244 254 264 274 284 294 [5,] 205 215 225 235 245 255 265 275 285 295 [6,] 206 216 226 236 246 256 266 276 286 296 [7,] 207 217 227 237 247 257 267 277 287 297 [8,] 208 218 228 238 248 258 268 278 288 298 [9,] 209 219 229 239 249 259 269 279 289 299 [10,] 210 220 230 240 250 260 270 280 290 300 ``` ] --- count: false .panel1-raster2-auto[ ```r array(1:300, dim = c(10,10,3)) %>% * brick() ``` ] .panel2-raster2-auto[ ``` class : RasterBrick dimensions : 10, 10, 100, 3 (nrow, ncol, ncell, nlayers) resolution : 0.1, 0.1 (x, y) extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax) crs : NA source : memory names : layer.1, layer.2, layer.3 min values : 1, 101, 201 max values : 100, 200, 300 ``` ] --- count: false .panel1-raster2-auto[ ```r array(1:300, dim = c(10,10,3)) %>% brick() %>% * rasterVis::levelplot() ``` ] .panel2-raster2-auto[ <img src="lecture-04_files/figure-html/raster2_auto_03_output-1.png" width="432" /> ] <style> .panel1-raster2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-raster2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-raster2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: center, middle ### [Assignment](https://github.com/mikejohnson51/exercise-04) --- <style type="text/css"> .remark-code{font-size: 100%} </style>