class: center, middle, inverse, title-slide .title[ # 02 R Data Structures ] .subtitle[ ## Elements of R e R Markdown ] .author[ ### Claudio Zandonella ] --- class: size-small # Math Operators .pull-left-50[ <table class="table table-striped table-hover" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Function </th> <th style="text-align:left;"> Name </th> <th style="text-align:left;"> Example </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> x + y </td> <td style="text-align:left;"> Addition </td> <td style="text-align:left;"> > 5 + 3 <br>[1] 8 </td> </tr> <tr> <td style="text-align:left;"> x - y </td> <td style="text-align:left;"> Subtraction </td> <td style="text-align:left;"> > 7 - 2 <br>[1] 5 </td> </tr> <tr> <td style="text-align:left;"> x * y </td> <td style="text-align:left;"> Multiplication </td> <td style="text-align:left;"> > 4 * 3 <br>[1] 12 </td> </tr> <tr> <td style="text-align:left;"> x / y </td> <td style="text-align:left;"> Division </td> <td style="text-align:left;"> > 8 / 3 <br>[1] 2.666667 </td> </tr> <tr> <td style="text-align:left;"> x %% y </td> <td style="text-align:left;"> Remainder from division </td> <td style="text-align:left;"> > 7 %% 5 <br>[1] 2 </td> </tr> <tr> <td style="text-align:left;"> x %/% y </td> <td style="text-align:left;"> Integer Division </td> <td style="text-align:left;"> > 7 %/% 5 <br>[1] 1 </td> </tr> <tr> <td style="text-align:left;"> x ^ y </td> <td style="text-align:left;"> Power </td> <td style="text-align:left;"> > 3 ^ 3 <br>[1] 27 </td> </tr> </tbody> </table> ] .pull-right-50[ <table class="table table-striped table-hover" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Function </th> <th style="text-align:left;"> Name </th> <th style="text-align:left;"> Example </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> abs(x) </td> <td style="text-align:left;"> Absolute value </td> <td style="text-align:left;"> > abs(3-5^2) <br>[1] 22 </td> </tr> <tr> <td style="text-align:left;"> sign(x) </td> <td style="text-align:left;"> Sign </td> <td style="text-align:left;"> > sign(-8) <br>[1] -1 </td> </tr> <tr> <td style="text-align:left;"> sqrt(x) </td> <td style="text-align:left;"> Square Root </td> <td style="text-align:left;"> > sqrt(25) <br>[1] 5 </td> </tr> <tr> <td style="text-align:left;"> log(x) </td> <td style="text-align:left;"> Logarithm (natural) </td> <td style="text-align:left;"> > log(10) <br>[1] 2.302585 </td> </tr> <tr> <td style="text-align:left;"> exp(x) </td> <td style="text-align:left;"> Exponential </td> <td style="text-align:left;"> > exp(1) <br>[1] 2.718282 </td> </tr> </tbody> </table> ] --- class: size-small # Relational and Logical Operators .pull-left-50[ <table class="table table-striped table-hover table-condensed table-responsive" style="width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Function </th> <th style="text-align:left;"> Name </th> <th style="text-align:left;"> Example </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> x == y </td> <td style="text-align:left;"> Equal to </td> <td style="text-align:left;"> > 5 == 3 <br>[1] FALSE </td> </tr> <tr> <td style="text-align:left;"> x != y </td> <td style="text-align:left;"> Not equal to </td> <td style="text-align:left;"> > 7 != 2 <br>[1] TRUE </td> </tr> <tr> <td style="text-align:left;"> x > y </td> <td style="text-align:left;"> Greater </td> <td style="text-align:left;"> > 4 > 3 <br>[1] TRUE </td> </tr> <tr> <td style="text-align:left;"> x >= y </td> <td style="text-align:left;"> Greater or equal </td> <td style="text-align:left;"> > -2 >= 3 <br>[1] FALSE </td> </tr> <tr> <td style="text-align:left;"> x < y </td> <td style="text-align:left;"> Less </td> <td style="text-align:left;"> > 7 < 5 <br>[1] FALSE </td> </tr> <tr> <td style="text-align:left;"> x <= y </td> <td style="text-align:left;"> Less or equal </td> <td style="text-align:left;"> > 7 <= 7 <br>[1] TRUE </td> </tr> <tr> <td style="text-align:left;"> x %in% y </td> <td style="text-align:left;"> Inclusion </td> <td style="text-align:left;"> > 5 %in% c(3, 5, 8) <br>[1] TRUE </td> </tr> </tbody> </table> ] .pull-right-50[ <table class="table table-striped table-hover table-condensed table-responsive" style="width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Function </th> <th style="text-align:left;"> Name </th> <th style="text-align:left;"> Example </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> x & y </td> <td style="text-align:left;"> And </td> <td style="text-align:left;"> > TRUE & TRUE <br>[1] TRUE<br>> TRUE & FALSE <br>[1] FALSE </td> </tr> <tr> <td style="text-align:left;"> x | y </td> <td style="text-align:left;"> Or (inclusive) </td> <td style="text-align:left;"> > TRUE & FALSE <br>[1] FALSE<br>> FALSE & FALSE <br>[1] FALSE </td> </tr> <tr> <td style="text-align:left;"> xor(x, y) </td> <td style="text-align:left;"> Or (disgiuntive) </td> <td style="text-align:left;"> > xor(TRUE, FALSE) <br>[1] TRUE<br>> xor(TRUE, TRUE) <br>[1] FALSE </td> </tr> <tr> <td style="text-align:left;"> !x </td> <td style="text-align:left;"> Negation </td> <td style="text-align:left;"> > !TRUE <br>[1] FALSE </td> </tr> </tbody> </table> ] --- class: size-small # Operators Order .pull-left-50[ 1. Math operators<br>(e.g., `^`, `*`, `/`, `+`, `-`, etc.) 2. Relational operators<br>(e.g., `<`, `>`, `<=`, `>=`, `==`, `!=`) 3. Logical operators<br>(e.g., `!`, `&`, `|`) ] .pull-right-50[ 1. `^` (Power) 2. `%%` (remainder from division) and<br>`%/%` (integer division) 3. `*` (multiplication) and<br>`/`(division) 4. `+` (addition) and<br>`-`(subraction) ] <div style="padding-top:250pt;"> <center><blockquote style ="width: fit-content;">Use parenthesis "`(...)`"!</blockquote></center> </div> --- class: size-small # Vectors .pull-left-50[ ### Creating ```r name_vector <- c(...) ``` ```r x <- c(2, 5, 1, 3, 7) x ## [1] 2 5 1 3 7 # Create a sequence with '<from>:<to>' y <- 1:8 y ## [1] 1 2 3 4 5 6 7 8 ``` ] .pull-right-50[ ### Selecting ```r name_vector[<index-position>] ``` ```r # Position index x[c(2, 5, 1)] ## [1] 5 7 2 # Logical condition x[x >= 5] ## [1] 5 7 # Removing x[- c(1, 3)] ## [1] 5 3 7 # Editing x[1] <- 9999 x ## [1] 9999 5 1 3 7 ``` ] --- class: size-small # Vectors .pull-left-50[ ### Types ```r # Character c("a", "b", "c") ## [1] "a" "b" "c" # Double c(3.5, 2, 9.34) ## [1] 3.50 2.00 9.34 # Integer c(1L, 2L, 3L) ## [1] 1 2 3 # Logical c(TRUE, FALSE, FALSE) ## [1] TRUE FALSE FALSE ``` ] .pull-right-50[ ### Function Families - `is.*()` test if vector of the correct type - `as.*()` coerces object to given type ### Special Values - `NULL`, the null object of length 0 (**important** `is.null()`) - `NA`, logical of length 1 indicating a missing value (**important** `is.na()`) - `Inf` (`-Inf`), numeric value indicating too large (or too small values) - `NaN`, numeric value indicating a number that can not be represented ] --- class: size-small # Common Functions .pull-left-50[ - `length()`, get object length ```r length(c("a", "b", "c")) ## [1] 3 ``` - `seq()`, sequence generation ```r seq(from = 0, to = 10, by = 2) ## [1] 0 2 4 6 8 10 ``` - `rep()`, replicate elements ```r rep(c("a", "b", "c"), each = 2) ## [1] "a" "a" "b" "b" "c" "c" ``` ] .pull-right-50[ - `sort()`, sort vector values ```r sort(c(6,3,8,1,3)) ## [1] 1 3 3 6 8 ``` - `order()`, return permutation which rearranges ```r order(c(6,3,8,1,3)) ## [1] 4 2 5 1 3 ``` - `order()`, get position of `TRUE` values ```r which(c(6,3,8,1,3) == 3) ## [1] 2 5 ``` ] --- class: size-small # Factors .pull-left-50[ ### Creating ```r factor(x = , levels = , ordered = FALSE) ``` ```r # Unordered factor x <- factor(c("a", "b", "a", "c")) x ## [1] a b a c ## Levels: a b c # Ordered factor, see also ?ordered() y <- factor( c("IV", "II", "I", "III"), levels = c("I", "II", "III", "IV"), ordered = TRUE) y ## [1] IV II I III ## Levels: I < II < III < IV ``` ] .pull-right-50[ ### Redefining Levels ```r levels(x) ## [1] "a" "b" "c" levels(x) <- c("A", "B", "C") x ## [1] A B A C ## Levels: A B C ``` ### Underline Structure ```r str(x) ## Factor w/ 3 levels "A","B","C": 1 2 1 3 as.numeric(x) ## [1] 1 2 1 3 ``` ] --- class: size-small # Matrices .pull-left-50[ ### Creating ```r matrix(data, nrow = , ncol = , byrow = FALSE) ``` ```r X <- matrix(1:12, ncol = 4) X ## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12 ``` ### Dimension ```r nrow(X) ## [1] 3 ncol(X) ## [1] 4 dim(X) ## [1] 3 4 ``` ] .pull-right-50[ ### Selecting ```r name_matrix[<row-index>, <col-index>] ``` ```r # Single element X[2, 3] ## [1] 8 # Entire row/column X[3, ] ## [1] 3 6 9 12 X[ , 2] ## [1] 4 5 6 # Subsetting X[1:2, 3:4] ## [,1] [,2] ## [1,] 7 10 ## [2,] 8 11 ``` ] --- class: size-small # Matrices .pull-left-50[ ### Rows and Cols Names ```r # Assigning names colnames(X) <- c("A", "B", "C", "D") rownames(X) <- c("a", "b", "c") X ## A B C D ## a 1 4 7 10 ## b 2 5 8 11 ## c 3 6 9 12 # Selecting by names X[ , "A"] ## a b c ## 1 2 3 ``` ] .pull-right-50[ ### Operations <table class="table table-striped table-hover table-condensed table-responsive" style="width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Functions </th> <th style="text-align:left;"> Description </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> cbind(X, Y) </td> <td style="text-align:left;"> Bind matrices by rows </td> </tr> <tr> <td style="text-align:left;"> rbind(X, Y) </td> <td style="text-align:left;"> Bind matrices by columns </td> </tr> <tr> <td style="text-align:left;"> t(X) </td> <td style="text-align:left;"> Matrix transpose </td> </tr> <tr> <td style="text-align:left;"> diag(X) </td> <td style="text-align:left;"> Get diagonal elements </td> </tr> <tr> <td style="text-align:left;"> det(X) </td> <td style="text-align:left;"> Determinant matrix </td> </tr> <tr> <td style="text-align:left;"> solve(X) </td> <td style="text-align:left;"> Inverse matrix </td> </tr> <tr> <td style="text-align:left;"> X + Y </td> <td style="text-align:left;"> Elementwise sum </td> </tr> <tr> <td style="text-align:left;"> X - Y </td> <td style="text-align:left;"> Elementwise difference </td> </tr> <tr> <td style="text-align:left;"> X * Y </td> <td style="text-align:left;"> Elementwise product </td> </tr> <tr> <td style="text-align:left;"> X / Y </td> <td style="text-align:left;"> Elementwise division </td> </tr> <tr> <td style="text-align:left;"> X %*% Y </td> <td style="text-align:left;"> Matrix product </td> </tr> </tbody> </table> ] --- class: size-small # Dataframe .pull-left-50[ ### Creating ```r data.frame(<var-1> = ..., <var-2> = ..., stringsAsFactors = FALSE) ``` ```r df <- data.frame( name = c("A", "B", "C", "D"), x = factor(c(0,1,0, 1)), y = 1:4) df ## name x y ## 1 A 0 1 ## 2 B 1 2 ## 3 C 0 3 ## 4 D 1 4 ``` ] .pull-right-50[ ### Structure ```r str(df) ## 'data.frame': 4 obs. of 3 variables: ## $ name: chr "A" "B" "C" "D" ## $ x : Factor w/ 2 levels "0","1": 1 2 1 2 ## $ y : int 1 2 3 4 ``` ### Summary ```r summary(df) ## name x y ## Length:4 0:2 Min. :1.00 ## Class :character 1:2 1st Qu.:1.75 ## Mode :character Median :2.50 ## Mean :2.50 ## 3rd Qu.:3.25 ## Max. :4.00 ``` ] --- class: size-small # Dataframe .pull-left-50[ ### Selecting ```r name_df[<row-index>, <col-index>] ``` ```r # Single value df[2, 1] ## [1] "B" # Entire rows/columns df[3:4, ] ## name x y ## 3 C 0 3 ## 4 D 1 4 df[ , 2:3] ## x y ## 1 0 1 ## 2 1 2 ## 3 0 3 ## 4 1 4 ``` ] .pull-right-50[ <br> ```r # By colnames df[, c("name", "y")] ## name y ## 1 A 1 ## 2 B 2 ## 3 C 3 ## 4 D 4 df$name ## [1] "A" "B" "C" "D" # Logical conditions df[df$y > 1, c("name", "y")] ## name y ## 2 B 2 ## 3 C 3 ## 4 D 4 ``` ] --- class: size-small # Dataframe .pull-left-50[ ### Rows and Cols Names ```r # Assigning names colnames(df) <- c("NAME", "X", "Y") rownames(df) <- c("a", "b", "c", "d") df ## NAME X Y ## a A 0 1 ## b B 1 2 ## c C 0 3 ## d D 1 4 ``` ] .pull-right-50[ ### Operations <table class="table table-striped table-hover table-condensed table-responsive" style="width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Function </th> <th style="text-align:left;"> Description </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> nrow(df) </td> <td style="text-align:left;"> Number of rows </td> </tr> <tr> <td style="text-align:left;"> ncol(df) </td> <td style="text-align:left;"> Number of columns </td> </tr> <tr> <td style="text-align:left;"> df$nome_var <- var_values </td> <td style="text-align:left;"> Create new column </td> </tr> <tr> <td style="text-align:left;"> cbind(df, dati) </td> <td style="text-align:left;"> Bind by columns </td> </tr> <tr> <td style="text-align:left;"> rbind(df, dati) </td> <td style="text-align:left;"> Bind by rows </td> </tr> <tr> <td style="text-align:left;"> head(df) </td> <td style="text-align:left;"> Return first rows </td> </tr> <tr> <td style="text-align:left;"> tail(df) </td> <td style="text-align:left;"> Return last rows </td> </tr> <tr> <td style="text-align:left;"> str(df) </td> <td style="text-align:left;"> Struttura del dataframe </td> </tr> <tr> <td style="text-align:left;"> summary(df) </td> <td style="text-align:left;"> Summary del dataframe </td> </tr> </tbody> </table> ] --- class: size-small # List .pull-left-50[ ### Creating ```r list(<item-1> = ..., <item-2> = ..., ...) ``` ```r my_list <- list(my_vector = x, my_matrix = X, my_df = df) # Number of items length(my_list) ## [1] 3 # Item names names(my_list) ## [1] "my_vector" "my_matrix" "my_df" ``` ] .pull-right-50[ ### Structure ```r str(my_list, max.level = 2) ## List of 3 ## $ my_vector: Factor w/ 3 levels "A","B","C": 1 2 1 3 ## $ my_matrix: int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ... ## ..- attr(*, "dimnames")=List of 2 ## $ my_df :'data.frame': 4 obs. of 3 variables: ## ..$ NAME: chr [1:4] "A" "B" "C" "D" ## ..$ X : Factor w/ 2 levels "0","1": 1 2 1 2 ## ..$ Y : int [1:4] 1 2 3 4 ``` ### Add element ```r my_list$new_item <- "Hello World!" c(my_list, new_item = "Hello World!") ``` ] --- class: size-small # List .pull-left-50[ ### Selecting (as list) ```r list_name[<name or position-index>] ``` ```r my_list[1] ## $my_vector ## [1] A B A C ## Levels: A B C my_list["my_vector"] ## $my_vector ## [1] A B A C ## Levels: A B C class(my_list[1]) ## [1] "list" ``` ] .pull-right-50[ ### Selecting (extract elementk) ```r list_name[[<name or position-index>]] list_name$<name> ``` ```r my_list[[1]] ## [1] A B A C ## Levels: A B C my_list[["my_vector"]] ## [1] A B A C ## Levels: A B C class(my_list$my_vector) ## [1] "factor" ``` ] --- class: end, center, middle # Thanks!