class: center, middle, inverse, title-slide # R Data Types and Transformations with dplyr ## PSYC 259: Principles of Data Science ### John Franchak --- # How can R data processing help us minimize mistakes? .center[<img src="types-of-errors.png" width="700">] --- # How can R data processing help us minimize mistakes? - Code *is* documentation - Transform data with less human intervention - But coding errors are also common...what can we do to make sure that our code works? * Instead of “hard” coding based on position (such as ds[1, 2]), filter and select by name and logical conditions * Use good coding practices to clean up variable names to make them human-readable * Reduce code duplication (copy-paste-tweak) with loops and functions * Write code that's reproducible (avoiding absolute file paths) --- # Data type and transformation tutorial - Data types - Logical statements - Introduction to *dplyr* <br /> <br /> .content-box-blue[ <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> Follow along from the [Github repo](https://github.com/psych-259-data-science-2022/259-data-transformations) ] .footnote[.small[.bold[Last updated: 2022-01-20]]] --- # Data types - Why have pre-defined types? * Allows software to efficiently store data in memory * If a value is an integer (1, 2, 3, 4) storing it as an integer makes calculations easier compared to storing it as a double (2.34542480424624086) - Allows software to implement rules about transformations * Addition/subtraction for a date follows different rules compared with integers/double * “Less than” makes sense when comparing numbers, but not when comparing strings --- # Common data types in R - Numeric * integer: 1, 2, 3 * double: 1.12124 - Character: “hello” - Logical: T/F (TRUE/FALSE) - Datetime - Factor - Use `typeof()` function to check type of a value, `str()` or `glimpse()` to check the types of each column of a tibble --- # Functions to test and convert types - Take a value and convert it into a different type * `as.numeric()`, `as.character()`, `as.date()` - Check if something is a particular type * `is.numeric()`, `is.character()` --- count: false # Data types .panel1-types-auto[ ```r *x <- 1 ``` ] .panel2-types-auto[ ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 *x ``` ] .panel2-types-auto[ ``` [1] 1 ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x *typeof(x) ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) *is.numeric(x) ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) *is.character(x) ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) is.character(x) *as.character(x) ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] "1" ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) is.character(x) as.character(x) *x + 1 ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] "1" ``` ``` [1] 2 ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) is.character(x) as.character(x) x + 1 *x <- "data.csv" ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] "1" ``` ``` [1] 2 ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) is.character(x) as.character(x) x + 1 x <- "data.csv" *typeof(x) ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] "1" ``` ``` [1] 2 ``` ``` [1] "character" ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) is.character(x) as.character(x) x + 1 x <- "data.csv" typeof(x) *is.numeric(x) ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] "1" ``` ``` [1] 2 ``` ``` [1] "character" ``` ``` [1] FALSE ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) is.character(x) as.character(x) x + 1 x <- "data.csv" typeof(x) is.numeric(x) *is.character(x) ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] "1" ``` ``` [1] 2 ``` ``` [1] "character" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) is.character(x) as.character(x) x + 1 x <- "data.csv" typeof(x) is.numeric(x) is.character(x) *as.numeric(x) ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] "1" ``` ``` [1] 2 ``` ``` [1] "character" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] NA ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) is.character(x) as.character(x) x + 1 x <- "data.csv" typeof(x) is.numeric(x) is.character(x) as.numeric(x) *#"data_raw" + x #"data_raw" + x ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] "1" ``` ``` [1] 2 ``` ``` [1] "character" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] NA ``` ] --- count: false # Data types .panel1-types-auto[ ```r x <- 1 x typeof(x) is.numeric(x) is.character(x) as.character(x) x + 1 x <- "data.csv" typeof(x) is.numeric(x) is.character(x) as.numeric(x) #"data_raw" + x #"data_raw" + x *paste0("data_raw/",x) ``` ] .panel2-types-auto[ ``` [1] 1 ``` ``` [1] "double" ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] "1" ``` ``` [1] 2 ``` ``` [1] "character" ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] NA ``` ``` [1] "data_raw/data.csv" ``` ] <style> .panel1-types-auto { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-types-auto { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-types-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Missing values - `NA` means missing data in R - `is.na()` checks whether a value is missing - not to be confused with `NULL` (empty), or `NaN` (not a number) --- count: false .panel1-missing-auto[ ```r *x <- c(1, 2, 3, NA) ``` ] .panel2-missing-auto[ ] --- count: false .panel1-missing-auto[ ```r x <- c(1, 2, 3, NA) *print(x) ``` ] .panel2-missing-auto[ ``` [1] 1 2 3 NA ``` ] --- count: false .panel1-missing-auto[ ```r x <- c(1, 2, 3, NA) print(x) *is.na(x) ``` ] .panel2-missing-auto[ ``` [1] 1 2 3 NA ``` ``` [1] FALSE FALSE FALSE TRUE ``` ] --- count: false .panel1-missing-auto[ ```r x <- c(1, 2, 3, NA) print(x) is.na(x) *mean(x) ``` ] .panel2-missing-auto[ ``` [1] 1 2 3 NA ``` ``` [1] FALSE FALSE FALSE TRUE ``` ``` [1] NA ``` ] --- count: false .panel1-missing-auto[ ```r x <- c(1, 2, 3, NA) print(x) is.na(x) mean(x) *mean(x, na.rm = TRUE) ``` ] .panel2-missing-auto[ ``` [1] 1 2 3 NA ``` ``` [1] FALSE FALSE FALSE TRUE ``` ``` [1] NA ``` ``` [1] 2 ``` ] --- count: false .panel1-missing-auto[ ```r x <- c(1, 2, 3, NA) print(x) is.na(x) mean(x) mean(x, na.rm = TRUE) *x <- c(1, 2, 3, NULL) ``` ] .panel2-missing-auto[ ``` [1] 1 2 3 NA ``` ``` [1] FALSE FALSE FALSE TRUE ``` ``` [1] NA ``` ``` [1] 2 ``` ] --- count: false .panel1-missing-auto[ ```r x <- c(1, 2, 3, NA) print(x) is.na(x) mean(x) mean(x, na.rm = TRUE) x <- c(1, 2, 3, NULL) *x ``` ] .panel2-missing-auto[ ``` [1] 1 2 3 NA ``` ``` [1] FALSE FALSE FALSE TRUE ``` ``` [1] NA ``` ``` [1] 2 ``` ``` [1] 1 2 3 ``` ] <style> .panel1-missing-auto { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-missing-auto { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-missing-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Logical comparisons - Comparisons between values that result in TRUE/FALSE - Greater than/less than (>, >=, <, <=) - Equals (==) - Not equals (!=) - Not (!) - And (&) - Or (|) - %in% --- count: false # Logical comparisons .panel1-logical-auto[ ```r *x <- 1 ``` ] .panel2-logical-auto[ ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 *x > 0 ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 *x > 2 ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 *x == 1 ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 x == 1 *x != 2 ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 x == 1 x != 2 *!(x == x) ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 x == 1 x != 2 !(x == x) *"s" == "S" ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 x == 1 x != 2 !(x == x) "s" == "S" *1 > 0 | 0 > 1 ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 x == 1 x != 2 !(x == x) "s" == "S" 1 > 0 | 0 > 1 *1 > 0 & 0 > 1 ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 x == 1 x != 2 !(x == x) "s" == "S" 1 > 0 | 0 > 1 1 > 0 & 0 > 1 # Element-wise logical statements *x <- c(-1, 0, 1) ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 x == 1 x != 2 !(x == x) "s" == "S" 1 > 0 | 0 > 1 1 > 0 & 0 > 1 # Element-wise logical statements x <- c(-1, 0, 1) *x < 0 ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE FALSE FALSE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 x == 1 x != 2 !(x == x) "s" == "S" 1 > 0 | 0 > 1 1 > 0 & 0 > 1 # Element-wise logical statements x <- c(-1, 0, 1) x < 0 *x == 0 ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE FALSE FALSE ``` ``` [1] FALSE TRUE FALSE ``` ] --- count: false # Logical comparisons .panel1-logical-auto[ ```r x <- 1 x > 0 x > 2 x == 1 x != 2 !(x == x) "s" == "S" 1 > 0 | 0 > 1 1 > 0 & 0 > 1 # Element-wise logical statements x <- c(-1, 0, 1) x < 0 x == 0 # Quickly test is a value is contained in a set *1 %in% x ``` ] .panel2-logical-auto[ ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] FALSE ``` ``` [1] TRUE ``` ``` [1] FALSE ``` ``` [1] TRUE FALSE FALSE ``` ``` [1] FALSE TRUE FALSE ``` ``` [1] TRUE ``` ] <style> .panel1-logical-auto { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-logical-auto { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-logical-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # How do we use logical statements? - One common way is through the `ifelse()` command? - `ifelse(LOGICAL STATEMENT, DO IF TRUE, DO IF FALSE)` ```r x <- c(0, 1, 2, 3, NA) ifelse(NA %in% x, "x contains a missing value", "x does not contain a missing value") ``` ``` [1] "x contains a missing value" ``` ```r x <- c(0, 1, 2, 3) ifelse(NA %in% x, "x contains a missing value", "x does not contain a missing value") ``` ``` [1] "x does not contain a missing value" ``` --- # Data transformation with the dplyr package - A toolbox for common data processing/manipulation operations to apply to tibbles * `glimpse()` to see the structure of a tibble * `arrange()` to sort data by columns * `filter()` and `slice()` to subset data by rows * `select()` to subset data by columns * `rename()` to rename columns * `mutate()` to add or change columns (or their values) * `summarize()` and `count()` to calculate summary statistics over rows of data * `group_by()` to perform operations within subsets of data * and many more... --- # Each dplyr function uses a similar structure - `function(data, something_to_do_with_columns)` - In base R, you often need to specify the dataset over and over to access the columns: `ds[ds$col1 == 1, 0]` - In dplyr functions, the data argument lets you work with column names without ever using `$`. The data argument also lets you access the columns directly without using quoted expressions: `filter(data, col1 == 1)` - Most dplyr functions return the entire tibble back as an output --- count: false ## Arrange (sorting data) .panel1-arrange-rotate[ ```r library(tidyverse) #loads dplyr ds <- starwars #loads built-in star wars database *glimpse(ds) ``` ] .panel2-arrange-rotate[ ``` Rows: 87 Columns: 14 $ name <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia Or… $ height <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188, 180, 2… $ mass <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 84.0, 77.… $ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "brown", N… $ skin_color <chr> "fair", "gold", "white, blue", "white", "light", "light", "… $ eye_color <chr> "blue", "yellow", "red", "yellow", "brown", "blue", "blue",… $ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0, 52.0, 47.0, NA, 24.0, 57.0, … $ sex <chr> "male", "none", "none", "male", "female", "male", "female",… $ gender <chr> "masculine", "masculine", "masculine", "masculine", "femini… $ homeworld <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", "T… $ species <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Huma… $ films <list> <"The Empire Strikes Back", "Revenge of the Sith", "Return… $ vehicles <list> <"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "Imp… $ starships <list> <"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x1",… ``` ] --- count: false ## Arrange (sorting data) .panel1-arrange-rotate[ ```r library(tidyverse) #loads dplyr ds <- starwars #loads built-in star wars database *arrange(ds, name) ``` ] .panel2-arrange-rotate[ ``` # A tibble: 87 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Ackbar 180 83 none brown mott… orange 41 male mascu… 2 Adi Ga… 184 50 none dark blue NA fema… femin… 3 Anakin… 188 84 blond fair blue 41.9 male mascu… 4 Arvel … NA NA brown fair brown NA male mascu… 5 Ayla S… 178 55 none blue hazel 48 fema… femin… 6 Bail P… 191 NA black tan brown 67 male mascu… 7 Barris… 166 50 black yellow blue 40 fema… femin… 8 BB8 NA NA none none black NA none mascu… 9 Ben Qu… 163 65 none grey, gree… orange NA male mascu… 10 Beru W… 165 75 brown light blue 47 fema… femin… # … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>, # films <list>, vehicles <list>, starships <list> ``` ] --- count: false ## Arrange (sorting data) .panel1-arrange-rotate[ ```r library(tidyverse) #loads dplyr ds <- starwars #loads built-in star wars database *arrange(ds, height) ``` ] .panel2-arrange-rotate[ ``` # A tibble: 87 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Yoda 66 17 white green brown 896 male mascu… 2 Ratts T… 79 15 none grey, blue unknown NA male mascu… 3 Wicket … 88 20 brown brown brown 8 male mascu… 4 Dud Bolt 94 45 none blue, grey yellow NA male mascu… 5 R2-D2 96 32 <NA> white, bl… red 33 none mascu… 6 R4-P17 96 NA none silver, r… red, blue NA none femin… 7 R5-D4 97 32 <NA> white, red red NA none mascu… 8 Sebulba 112 40 none grey, red orange NA male mascu… 9 Gasgano 122 NA none white, bl… black NA male mascu… 10 Watto 137 NA black blue, grey yellow NA male mascu… # … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>, # films <list>, vehicles <list>, starships <list> ``` ] --- count: false ## Arrange (sorting data) .panel1-arrange-rotate[ ```r library(tidyverse) #loads dplyr ds <- starwars #loads built-in star wars database *arrange(ds, desc(height), mass) ``` ] .panel2-arrange-rotate[ ``` # A tibble: 87 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Yarael… 264 NA none white yellow NA male mascu… 2 Tarfful 234 136 brown brown blue NA male mascu… 3 Lama Su 229 88 none grey black NA male mascu… 4 Chewba… 228 112 brown unknown blue 200 male mascu… 5 Roos T… 224 82 none grey orange NA male mascu… 6 Grievo… 216 159 none brown, whi… green, y… NA male mascu… 7 Taun We 213 NA none grey black NA fema… femin… 8 Tion M… 206 80 none grey black NA male mascu… 9 Rugor … 206 NA none green orange NA male mascu… 10 Darth … 202 136 none white yellow 41.9 male mascu… # … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>, # films <list>, vehicles <list>, starships <list> ``` ] --- count: false ## Arrange (sorting data) .panel1-arrange-rotate[ ```r library(tidyverse) #loads dplyr ds <- starwars #loads built-in star wars database *arrange(ds, eye_color, hair_color) ``` ] .panel2-arrange-rotate[ ``` # A tibble: 87 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Nien … 160 68 none grey black NA male mascu… 2 Gasga… 122 NA none white, blue black NA male mascu… 3 Kit F… 196 87 none green black NA male mascu… 4 Plo K… 188 80 none orange black 22 male mascu… 5 Lama … 229 88 none grey black NA male mascu… 6 Taun … 213 NA none grey black NA fema… femin… 7 Shaak… 178 57 none red, blue, … black NA fema… femin… 8 Tion … 206 80 none grey black NA male mascu… 9 BB8 NA NA none none black NA none mascu… 10 Greedo 173 74 <NA> green black 44 male mascu… # … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>, # films <list>, vehicles <list>, starships <list> ``` ] <style> .panel1-arrange-rotate { color: black; width: 81.6666666666667%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel2-arrange-rotate { color: black; width: 16.3333333333333%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel3-arrange-rotate { color: black; width: NA%; hight: 33%; float: none; padding-left: 1%; font-size: 80% } </style> --- count: false # Filter (subsetting rows) .panel1-filter-rotate[ ```r *filter(ds, height < 100) ``` ] .panel2-filter-rotate[ ``` # A tibble: 7 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 R2-D2 96 32 <NA> white, bl… red 33 none mascu… 2 R5-D4 97 32 <NA> white, red red NA none mascu… 3 Yoda 66 17 white green brown 896 male mascu… 4 Wicket S… 88 20 brown brown brown 8 male mascu… 5 Dud Bolt 94 45 none blue, grey yellow NA male mascu… 6 Ratts Ty… 79 15 none grey, blue unknown NA male mascu… 7 R4-P17 96 NA none silver, r… red, blue NA none femin… # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # Filter (subsetting rows) .panel1-filter-rotate[ ```r *filter(ds, name == "Yoda") ``` ] .panel2-filter-rotate[ ``` # A tibble: 1 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Yoda 66 17 white green brown 896 male masculine # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # Filter (subsetting rows) .panel1-filter-rotate[ ```r *filter(ds, is.na(hair_color)) ``` ] .panel2-filter-rotate[ ``` # A tibble: 5 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 C-3PO 167 75 <NA> gold yellow 112 none mascu… 2 R2-D2 96 32 <NA> white, blue red 33 none mascu… 3 R5-D4 97 32 <NA> white, red red NA none mascu… 4 Greedo 173 74 <NA> green black 44 male mascu… 5 Jabba … 175 1358 <NA> green-tan,… orange 600 herma… mascu… # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # Filter (subsetting rows) .panel1-filter-rotate[ ```r *filter(ds, height > 100, height < 150) ``` ] .panel2-filter-rotate[ ``` # A tibble: 3 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Watto 137 NA black blue, grey yellow NA male mascul… 2 Sebulba 112 40 none grey, red orange NA male mascul… 3 Gasgano 122 NA none white, blue black NA male mascul… # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # Filter (subsetting rows) .panel1-filter-rotate[ ```r *filter(ds, eye_color %in% c("blue","brown")) ``` ] .panel2-filter-rotate[ ``` # A tibble: 40 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Luke Sk… 172 77 blond fair blue 19 male mascu… 2 Leia Or… 150 49 brown light brown 19 fema… femin… 3 Owen La… 178 120 brown, gr… light blue 52 male mascu… 4 Beru Wh… 165 75 brown light blue 47 fema… femin… 5 Biggs D… 183 84 black light brown 24 male mascu… 6 Anakin … 188 84 blond fair blue 41.9 male mascu… 7 Wilhuff… 180 NA auburn, g… fair blue 64 male mascu… 8 Chewbac… 228 112 brown unknown blue 200 male mascu… 9 Han Solo 180 80 brown fair brown 29 male mascu… 10 Jek Ton… 180 110 brown fair blue NA male mascu… # … with 30 more rows, and 5 more variables: homeworld <chr>, species <chr>, # films <list>, vehicles <list>, starships <list> ``` ] --- count: false # Filter (subsetting rows) .panel1-filter-rotate[ ```r *filter(ds, !(eye_color %in% c("blue","brown"))) ``` ] .panel2-filter-rotate[ ``` # A tibble: 47 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 C-3PO 167 75 <NA> gold yellow 112 none mascu… 2 R2-D2 96 32 <NA> white, bl… red 33 none mascu… 3 Darth … 202 136 none white yellow 41.9 male mascu… 4 R5-D4 97 32 <NA> white, red red NA none mascu… 5 Obi-Wa… 182 77 auburn, wh… fair blue-gray 57 male mascu… 6 Greedo 173 74 <NA> green black 44 male mascu… 7 Jabba … 175 1358 <NA> green-tan… orange 600 herm… mascu… 8 Wedge … 170 77 brown fair hazel 21 male mascu… 9 Palpat… 170 75 grey pale yellow 82 male mascu… 10 IG-88 200 140 none metal red 15 none mascu… # … with 37 more rows, and 5 more variables: homeworld <chr>, species <chr>, # films <list>, vehicles <list>, starships <list> ``` ] <style> .panel1-filter-rotate { color: black; width: 81.6666666666667%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel2-filter-rotate { color: black; width: 16.3333333333333%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel3-filter-rotate { color: black; width: NA%; hight: 33%; float: none; padding-left: 1%; font-size: 80% } </style> --- count: false # Slice (subsetting rows by position) .panel1-slice-rotate[ ```r *slice(ds, 1:5) ``` ] .panel2-slice-rotate[ ``` # A tibble: 5 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Luke Sk… 172 77 blond fair blue 19 male mascu… 2 C-3PO 167 75 <NA> gold yellow 112 none mascu… 3 R2-D2 96 32 <NA> white, blue red 33 none mascu… 4 Darth V… 202 136 none white yellow 41.9 male mascu… 5 Leia Or… 150 49 brown light brown 19 fema… femin… # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # Slice (subsetting rows by position) .panel1-slice-rotate[ ```r *slice_head(ds, n = 5) ``` ] .panel2-slice-rotate[ ``` # A tibble: 5 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Luke Sk… 172 77 blond fair blue 19 male mascu… 2 C-3PO 167 75 <NA> gold yellow 112 none mascu… 3 R2-D2 96 32 <NA> white, blue red 33 none mascu… 4 Darth V… 202 136 none white yellow 41.9 male mascu… 5 Leia Or… 150 49 brown light brown 19 fema… femin… # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # Slice (subsetting rows by position) .panel1-slice-rotate[ ```r *slice_tail(ds, n = 4) ``` ] .panel2-slice-rotate[ ``` # A tibble: 4 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Poe Dam… NA NA brown light brown NA male mascu… 2 BB8 NA NA none none black NA none mascu… 3 Captain… NA NA unknown unknown unknown NA <NA> <NA> 4 Padmé A… 165 45 brown light brown 46 female femin… # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # Slice (subsetting rows by position) .panel1-slice-rotate[ ```r *slice_sample(ds, n = 2) ``` ] .panel2-slice-rotate[ ``` # A tibble: 2 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Mace Wi… 188 84 none dark brown 72 male mascul… 2 Sly Moo… 178 48 none pale white NA <NA> <NA> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # Slice (subsetting rows by position) .panel1-slice-rotate[ ```r *slice_sample(ds, n = 2) ``` ] .panel2-slice-rotate[ ``` # A tibble: 2 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Ki-Adi-… 198 82 white pale yellow 92 male mascul… 2 Sebulba 112 40 none grey, red orange NA male mascul… # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # Slice (subsetting rows by position) .panel1-slice-rotate[ ```r *slice_min(ds, height, n = 3) ``` ] .panel2-slice-rotate[ ``` # A tibble: 3 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Yoda 66 17 white green brown 896 male mascu… 2 Ratts Ty… 79 15 none grey, blue unknown NA male mascu… 3 Wicket S… 88 20 brown brown brown 8 male mascu… # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] <style> .panel1-slice-rotate { color: black; width: 81.6666666666667%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel2-slice-rotate { color: black; width: 16.3333333333333%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel3-slice-rotate { color: black; width: NA%; hight: 33%; float: none; padding-left: 1%; font-size: 80% } </style> --- count: false # Select (subsetting columns) .panel1-select-rotate[ ```r *select(ds, name) ``` ] .panel2-select-rotate[ ``` # A tibble: 87 × 1 name <chr> 1 Luke Skywalker 2 C-3PO 3 R2-D2 4 Darth Vader 5 Leia Organa 6 Owen Lars 7 Beru Whitesun lars 8 R5-D4 9 Biggs Darklighter 10 Obi-Wan Kenobi # … with 77 more rows ``` ] --- count: false # Select (subsetting columns) .panel1-select-rotate[ ```r *select(ds, name, height, mass) ``` ] .panel2-select-rotate[ ``` # A tibble: 87 × 3 name height mass <chr> <int> <dbl> 1 Luke Skywalker 172 77 2 C-3PO 167 75 3 R2-D2 96 32 4 Darth Vader 202 136 5 Leia Organa 150 49 6 Owen Lars 178 120 7 Beru Whitesun lars 165 75 8 R5-D4 97 32 9 Biggs Darklighter 183 84 10 Obi-Wan Kenobi 182 77 # … with 77 more rows ``` ] --- count: false # Select (subsetting columns) .panel1-select-rotate[ ```r *select(ds, c("name", "height", "mass")) ``` ] .panel2-select-rotate[ ``` # A tibble: 87 × 3 name height mass <chr> <int> <dbl> 1 Luke Skywalker 172 77 2 C-3PO 167 75 3 R2-D2 96 32 4 Darth Vader 202 136 5 Leia Organa 150 49 6 Owen Lars 178 120 7 Beru Whitesun lars 165 75 8 R5-D4 97 32 9 Biggs Darklighter 183 84 10 Obi-Wan Kenobi 182 77 # … with 77 more rows ``` ] --- count: false # Select (subsetting columns) .panel1-select-rotate[ ```r *select(ds, name:eye_color) ``` ] .panel2-select-rotate[ ``` # A tibble: 87 × 6 name height mass hair_color skin_color eye_color <chr> <int> <dbl> <chr> <chr> <chr> 1 Luke Skywalker 172 77 blond fair blue 2 C-3PO 167 75 <NA> gold yellow 3 R2-D2 96 32 <NA> white, blue red 4 Darth Vader 202 136 none white yellow 5 Leia Organa 150 49 brown light brown 6 Owen Lars 178 120 brown, grey light blue 7 Beru Whitesun lars 165 75 brown light blue 8 R5-D4 97 32 <NA> white, red red 9 Biggs Darklighter 183 84 black light brown 10 Obi-Wan Kenobi 182 77 auburn, white fair blue-gray # … with 77 more rows ``` ] --- count: false # Select (subsetting columns) .panel1-select-rotate[ ```r *select(ds, -(eye_color:starships)) ``` ] .panel2-select-rotate[ ``` # A tibble: 87 × 5 name height mass hair_color skin_color <chr> <int> <dbl> <chr> <chr> 1 Luke Skywalker 172 77 blond fair 2 C-3PO 167 75 <NA> gold 3 R2-D2 96 32 <NA> white, blue 4 Darth Vader 202 136 none white 5 Leia Organa 150 49 brown light 6 Owen Lars 178 120 brown, grey light 7 Beru Whitesun lars 165 75 brown light 8 R5-D4 97 32 <NA> white, red 9 Biggs Darklighter 183 84 black light 10 Obi-Wan Kenobi 182 77 auburn, white fair # … with 77 more rows ``` ] --- count: false # Select (subsetting columns) .panel1-select-rotate[ ```r *select(ds, ends_with("color")) ``` ] .panel2-select-rotate[ ``` # A tibble: 87 × 3 hair_color skin_color eye_color <chr> <chr> <chr> 1 blond fair blue 2 <NA> gold yellow 3 <NA> white, blue red 4 none white yellow 5 brown light brown 6 brown, grey light blue 7 brown light blue 8 <NA> white, red red 9 black light brown 10 auburn, white fair blue-gray # … with 77 more rows ``` ] --- count: false # Select (subsetting columns) .panel1-select-rotate[ ```r *select(ds, contains("_")) ``` ] .panel2-select-rotate[ ``` # A tibble: 87 × 4 hair_color skin_color eye_color birth_year <chr> <chr> <chr> <dbl> 1 blond fair blue 19 2 <NA> gold yellow 112 3 <NA> white, blue red 33 4 none white yellow 41.9 5 brown light brown 19 6 brown, grey light blue 52 7 brown light blue 47 8 <NA> white, red red NA 9 black light brown 24 10 auburn, white fair blue-gray 57 # … with 77 more rows ``` ] --- count: false # Select (subsetting columns) .panel1-select-rotate[ ```r *select(ds, where(is.numeric)) ``` ] .panel2-select-rotate[ ``` # A tibble: 87 × 3 height mass birth_year <int> <dbl> <dbl> 1 172 77 19 2 167 75 112 3 96 32 33 4 202 136 41.9 5 150 49 19 6 178 120 52 7 165 75 47 8 97 32 NA 9 183 84 24 10 182 77 57 # … with 77 more rows ``` ] --- count: false # Select (subsetting columns) .panel1-select-rotate[ ```r *select(ds, where(is.character)) ``` ] .panel2-select-rotate[ ``` # A tibble: 87 × 8 name hair_color skin_color eye_color sex gender homeworld species <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> 1 Luke Skywa… blond fair blue male mascul… Tatooine Human 2 C-3PO <NA> gold yellow none mascul… Tatooine Droid 3 R2-D2 <NA> white, bl… red none mascul… Naboo Droid 4 Darth Vader none white yellow male mascul… Tatooine Human 5 Leia Organa brown light brown fema… femini… Alderaan Human 6 Owen Lars brown, grey light blue male mascul… Tatooine Human 7 Beru White… brown light blue fema… femini… Tatooine Human 8 R5-D4 <NA> white, red red none mascul… Tatooine Droid 9 Biggs Dark… black light brown male mascul… Tatooine Human 10 Obi-Wan Ke… auburn, whi… fair blue-gray male mascul… Stewjon Human # … with 77 more rows ``` ] <style> .panel1-select-rotate { color: black; width: 81.6666666666667%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel2-select-rotate { color: black; width: 16.3333333333333%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel3-select-rotate { color: black; width: NA%; hight: 33%; float: none; padding-left: 1%; font-size: 80% } </style> --- count: false # What's going on here? .panel1-noassignment-auto[ ```r *select(ds, name, height, eye_color) ``` ] .panel2-noassignment-auto[ ``` # A tibble: 87 × 3 name height eye_color <chr> <int> <chr> 1 Luke Skywalker 172 blue 2 C-3PO 167 yellow 3 R2-D2 96 red 4 Darth Vader 202 yellow 5 Leia Organa 150 brown 6 Owen Lars 178 blue 7 Beru Whitesun lars 165 blue 8 R5-D4 97 red 9 Biggs Darklighter 183 brown 10 Obi-Wan Kenobi 182 blue-gray # … with 77 more rows ``` ] --- count: false # What's going on here? .panel1-noassignment-auto[ ```r select(ds, name, height, eye_color) *filter(ds, height < 70) ``` ] .panel2-noassignment-auto[ ``` # A tibble: 87 × 3 name height eye_color <chr> <int> <chr> 1 Luke Skywalker 172 blue 2 C-3PO 167 yellow 3 R2-D2 96 red 4 Darth Vader 202 yellow 5 Leia Organa 150 brown 6 Owen Lars 178 blue 7 Beru Whitesun lars 165 blue 8 R5-D4 97 red 9 Biggs Darklighter 183 brown 10 Obi-Wan Kenobi 182 blue-gray # … with 77 more rows ``` ``` # A tibble: 1 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Yoda 66 17 white green brown 896 male masculine # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ] --- count: false # What's going on here? .panel1-noassignment-auto[ ```r select(ds, name, height, eye_color) filter(ds, height < 70) *ds ``` ] .panel2-noassignment-auto[ ``` # A tibble: 87 × 3 name height eye_color <chr> <int> <chr> 1 Luke Skywalker 172 blue 2 C-3PO 167 yellow 3 R2-D2 96 red 4 Darth Vader 202 yellow 5 Leia Organa 150 brown 6 Owen Lars 178 blue 7 Beru Whitesun lars 165 blue 8 R5-D4 97 red 9 Biggs Darklighter 183 brown 10 Obi-Wan Kenobi 182 blue-gray # … with 77 more rows ``` ``` # A tibble: 1 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Yoda 66 17 white green brown 896 male masculine # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> ``` ``` # A tibble: 87 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Luke S… 172 77 blond fair blue 19 male mascu… 2 C-3PO 167 75 <NA> gold yellow 112 none mascu… 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu… 4 Darth … 202 136 none white yellow 41.9 male mascu… 5 Leia O… 150 49 brown light brown 19 fema… femin… 6 Owen L… 178 120 brown, grey light blue 52 male mascu… 7 Beru W… 165 75 brown light blue 47 fema… femin… 8 R5-D4 97 32 <NA> white, red red NA none mascu… 9 Biggs … 183 84 black light brown 24 male mascu… 10 Obi-Wa… 182 77 auburn, wh… fair blue-gray 57 male mascu… # … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>, # films <list>, vehicles <list>, starships <list> ``` ] <style> .panel1-noassignment-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-noassignment-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-noassignment-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Reassign the transformations back to the tibble .panel1-reassignment-auto[ ```r *ds <- select(ds, name, height, eye_color) ds ``` ] .panel2-reassignment-auto[ ``` # A tibble: 87 × 3 name height eye_color <chr> <int> <chr> 1 Luke Skywalker 172 blue 2 C-3PO 167 yellow 3 R2-D2 96 red 4 Darth Vader 202 yellow 5 Leia Organa 150 brown 6 Owen Lars 178 blue 7 Beru Whitesun lars 165 blue 8 R5-D4 97 red 9 Biggs Darklighter 183 brown 10 Obi-Wan Kenobi 182 blue-gray # … with 77 more rows ``` ] --- count: false # Reassign the transformations back to the tibble .panel1-reassignment-auto[ ```r ds <- select(ds, name, height, eye_color) *ds <- arrange(ds, height, eye_color) ds ``` ] .panel2-reassignment-auto[ ``` # A tibble: 87 × 3 name height eye_color <chr> <int> <chr> 1 Yoda 66 brown 2 Ratts Tyerell 79 unknown 3 Wicket Systri Warrick 88 brown 4 Dud Bolt 94 yellow 5 R2-D2 96 red 6 R4-P17 96 red, blue 7 R5-D4 97 red 8 Sebulba 112 orange 9 Gasgano 122 black 10 Watto 137 yellow # … with 77 more rows ``` ] --- count: false # Reassign the transformations back to the tibble .panel1-reassignment-auto[ ```r ds <- select(ds, name, height, eye_color) ds <- arrange(ds, height, eye_color) *ds <- filter(ds, height < 70) ds ``` ] .panel2-reassignment-auto[ ``` # A tibble: 1 × 3 name height eye_color <chr> <int> <chr> 1 Yoda 66 brown ``` ] <style> .panel1-reassignment-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-reassignment-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-reassignment-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Not a good strategy .panel1-intermed-auto[ ```r *ds <- starwars ``` ] .panel2-intermed-auto[ ] --- count: false # Not a good strategy .panel1-intermed-auto[ ```r ds <- starwars *ds_name_height_eye_color <- select(ds, name, height, eye_color) ``` ] .panel2-intermed-auto[ ] --- count: false # Not a good strategy .panel1-intermed-auto[ ```r ds <- starwars ds_name_height_eye_color <- select(ds, name, height, eye_color) *ds_sorted <- arrange(ds_name_height_eye_color, height, eye_color) ``` ] .panel2-intermed-auto[ ] --- count: false # Not a good strategy .panel1-intermed-auto[ ```r ds <- starwars ds_name_height_eye_color <- select(ds, name, height, eye_color) ds_sorted <- arrange(ds_name_height_eye_color, height, eye_color) *ds_sorted_filtered <- filter(ds_sorted, height < 70) ``` ] .panel2-intermed-auto[ ] --- count: false # Not a good strategy .panel1-intermed-auto[ ```r ds <- starwars ds_name_height_eye_color <- select(ds, name, height, eye_color) ds_sorted <- arrange(ds_name_height_eye_color, height, eye_color) ds_sorted_filtered <- filter(ds_sorted, height < 70) *ds_sorted_filtered ``` ] .panel2-intermed-auto[ ``` # A tibble: 1 × 3 name height eye_color <chr> <int> <chr> 1 Yoda 66 brown ``` ] <style> .panel1-intermed-auto { color: black; width: 81.6666666666667%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel2-intermed-auto { color: black; width: 16.3333333333333%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel3-intermed-auto { color: black; width: NA%; hight: 33%; float: none; padding-left: 1%; font-size: 80% } </style> --- count: false # Introducing the pipe operator: %>% .panel1-pipe-auto[ ```r *ds <- starwars ds ``` ] .panel2-pipe-auto[ ``` # A tibble: 87 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Luke S… 172 77 blond fair blue 19 male mascu… 2 C-3PO 167 75 <NA> gold yellow 112 none mascu… 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu… 4 Darth … 202 136 none white yellow 41.9 male mascu… 5 Leia O… 150 49 brown light brown 19 fema… femin… 6 Owen L… 178 120 brown, grey light blue 52 male mascu… 7 Beru W… 165 75 brown light blue 47 fema… femin… 8 R5-D4 97 32 <NA> white, red red NA none mascu… 9 Biggs … 183 84 black light brown 24 male mascu… 10 Obi-Wa… 182 77 auburn, wh… fair blue-gray 57 male mascu… # … with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>, # films <list>, vehicles <list>, starships <list> ``` ] --- count: false # Introducing the pipe operator: %>% .panel1-pipe-auto[ ```r ds <- starwars *ds <- ds %>% select(name, height, eye_color) ds ``` ] .panel2-pipe-auto[ ``` # A tibble: 87 × 3 name height eye_color <chr> <int> <chr> 1 Luke Skywalker 172 blue 2 C-3PO 167 yellow 3 R2-D2 96 red 4 Darth Vader 202 yellow 5 Leia Organa 150 brown 6 Owen Lars 178 blue 7 Beru Whitesun lars 165 blue 8 R5-D4 97 red 9 Biggs Darklighter 183 brown 10 Obi-Wan Kenobi 182 blue-gray # … with 77 more rows ``` ] --- count: false # Introducing the pipe operator: %>% .panel1-pipe-auto[ ```r ds <- starwars ds <- ds %>% select(name, height, eye_color) *ds <- ds ds ``` ] .panel2-pipe-auto[ ``` # A tibble: 87 × 3 name height eye_color <chr> <int> <chr> 1 Luke Skywalker 172 blue 2 C-3PO 167 yellow 3 R2-D2 96 red 4 Darth Vader 202 yellow 5 Leia Organa 150 brown 6 Owen Lars 178 blue 7 Beru Whitesun lars 165 blue 8 R5-D4 97 red 9 Biggs Darklighter 183 brown 10 Obi-Wan Kenobi 182 blue-gray # … with 77 more rows ``` ] --- count: false # Introducing the pipe operator: %>% .panel1-pipe-auto[ ```r ds <- starwars ds <- ds %>% select(name, height, eye_color) ds <- ds %>% * select(name, eye_color) ds ``` ] .panel2-pipe-auto[ ``` # A tibble: 87 × 2 name eye_color <chr> <chr> 1 Luke Skywalker blue 2 C-3PO yellow 3 R2-D2 red 4 Darth Vader yellow 5 Leia Organa brown 6 Owen Lars blue 7 Beru Whitesun lars blue 8 R5-D4 red 9 Biggs Darklighter brown 10 Obi-Wan Kenobi blue-gray # … with 77 more rows ``` ] --- count: false # Introducing the pipe operator: %>% .panel1-pipe-auto[ ```r ds <- starwars ds <- ds %>% select(name, height, eye_color) ds <- ds %>% select(name, eye_color) %>% * arrange(eye_color) ds ``` ] .panel2-pipe-auto[ ``` # A tibble: 87 × 2 name eye_color <chr> <chr> 1 Greedo black 2 Nien Nunb black 3 Gasgano black 4 Kit Fisto black 5 Plo Koon black 6 Lama Su black 7 Taun We black 8 Shaak Ti black 9 Tion Medon black 10 BB8 black # … with 77 more rows ``` ] --- count: false # Introducing the pipe operator: %>% .panel1-pipe-auto[ ```r ds <- starwars ds <- ds %>% select(name, height, eye_color) ds <- ds %>% select(name, eye_color) %>% arrange(eye_color) %>% * filter(eye_color == "blue") ds ``` ] .panel2-pipe-auto[ ``` # A tibble: 19 × 2 name eye_color <chr> <chr> 1 Luke Skywalker blue 2 Owen Lars blue 3 Beru Whitesun lars blue 4 Anakin Skywalker blue 5 Wilhuff Tarkin blue 6 Chewbacca blue 7 Jek Tono Porkins blue 8 Lobot blue 9 Mon Mothma blue 10 Qui-Gon Jinn blue 11 Finis Valorum blue 12 Ric Olié blue 13 Adi Gallia blue 14 Mas Amedda blue 15 Cliegg Lars blue 16 Luminara Unduli blue 17 Barriss Offee blue 18 Jocasta Nu blue 19 Tarfful blue ``` ] <style> .panel1-pipe-auto { color: black; width: 81.6666666666667%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel2-pipe-auto { color: black; width: 16.3333333333333%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel3-pipe-auto { color: black; width: NA%; hight: 33%; float: none; padding-left: 1%; font-size: 80% } </style> --- # This is not the most intuitive thing at first All the options below are equivalent, but I stick with `ds <- ds %>%` because it's the most common to see ```r ds <- ds %>% select(height) %>% slice_tail(n = 5) ds %>% select(height) %>% slice_tail(n = 5) -> ds ds <- select(ds, height) %>% slice_tail(n = 5) ds <- slice_tail(select(ds, height), n = 5) ``` --- # Rename - Consistent and clear names help prevent errors * Avoid names like "dv1", "dv2" that are difficult to remember * Auto-complete within RStudio means that you don't usually need to type out longer names - Using rename can help clean up messy names from other sources * Avoid names that contain spaces or start with numbers * Force names to use a similar format like snake_case or camelCase - Installing and loading the `janitor` package opens up some helpful renaming options --- count: false # Rename columns .panel1-rename-rotate[ ```r library(janitor) # Switching to built-in iris data set since starwars has good names *iris ``` ] .panel2-rename-rotate[ ``` 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 ``` ] --- count: false # Rename columns .panel1-rename-rotate[ ```r library(janitor) # Switching to built-in iris data set since starwars has good names *iris %>% rename(sepal_length = Sepal.Length) ``` ] .panel2-rename-rotate[ ``` 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 ``` ] --- count: false # Rename columns .panel1-rename-rotate[ ```r library(janitor) # Switching to built-in iris data set since starwars has good names *iris %>% rename(sepal_length = Sepal.Length, sepal_width = Sepal.Width, petal_length = Petal.Length, petal_width = Petal.Width, species = Species) ``` ] .panel2-rename-rotate[ ``` 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 ``` ] --- count: false # Rename columns .panel1-rename-rotate[ ```r library(janitor) # Switching to built-in iris data set since starwars has good names *iris %>% rename_with(toupper) ``` ] .panel2-rename-rotate[ ``` 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 ``` ] --- count: false # Rename columns .panel1-rename-rotate[ ```r library(janitor) # Switching to built-in iris data set since starwars has good names *iris %>% rename_with(tolower, starts_with("Petal")) ``` ] .panel2-rename-rotate[ ``` 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 ``` ] --- count: false # Rename columns .panel1-rename-rotate[ ```r library(janitor) # Switching to built-in iris data set since starwars has good names *iris %>% clean_names() ``` ] .panel2-rename-rotate[ ``` 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 ``` ] --- count: false # Rename columns .panel1-rename-rotate[ ```r library(janitor) # Switching to built-in iris data set since starwars has good names *iris %>% clean_names("small_camel") ``` ] .panel2-rename-rotate[ ``` sepalLength sepalWidth petalLength petalWidth 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 ``` ] <style> .panel1-rename-rotate { color: black; width: 81.6666666666667%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel2-rename-rotate { color: black; width: 16.3333333333333%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel3-rename-rotate { color: black; width: NA%; hight: 33%; float: none; padding-left: 1%; font-size: 80% } </style> --- # Mutate and summarize - Mutate adds/modifies columns; dataset remains the same size - Summarize collapses the data set down (by default to 1 row) to calculate summary statistics - Both functions use a similar form: * `mutate(data, new_variable = expression)` * `summarize(data, mean = mean(var))` - Like all other dplyr functions, they return tibbles, so for mutate to "stick" it needs to be assigned back to itself: * `data <- mutate(data, var = round(var))` * `data <- mutate(data, var_r = round(var))` - Since summarize collapses to a new level, you might not want to assign it to overwrite the original data set! --- count: false # Mutate (add or modify columns) .panel1-mutate-auto[ ```r *ds <- starwars %>% select(name, mass, height, hair_color) ds ``` ] .panel2-mutate-auto[ ``` # A tibble: 87 × 4 name mass height hair_color <chr> <dbl> <int> <chr> 1 Luke Skywalker 77 172 blond 2 C-3PO 75 167 <NA> 3 R2-D2 32 96 <NA> 4 Darth Vader 136 202 none 5 Leia Organa 49 150 brown 6 Owen Lars 120 178 brown, grey 7 Beru Whitesun lars 75 165 brown 8 R5-D4 32 97 <NA> 9 Biggs Darklighter 84 183 black 10 Obi-Wan Kenobi 77 182 auburn, white # … with 77 more rows ``` ] --- count: false # Mutate (add or modify columns) .panel1-mutate-auto[ ```r ds <- starwars %>% select(name, mass, height, hair_color) *ds <- ds %>% mutate(in_movie = "yes") ds ``` ] .panel2-mutate-auto[ ``` # A tibble: 87 × 5 name mass height hair_color in_movie <chr> <dbl> <int> <chr> <chr> 1 Luke Skywalker 77 172 blond yes 2 C-3PO 75 167 <NA> yes 3 R2-D2 32 96 <NA> yes 4 Darth Vader 136 202 none yes 5 Leia Organa 49 150 brown yes 6 Owen Lars 120 178 brown, grey yes 7 Beru Whitesun lars 75 165 brown yes 8 R5-D4 32 97 <NA> yes 9 Biggs Darklighter 84 183 black yes 10 Obi-Wan Kenobi 77 182 auburn, white yes # … with 77 more rows ``` ] --- count: false # Mutate (add or modify columns) .panel1-mutate-auto[ ```r ds <- starwars %>% select(name, mass, height, hair_color) ds <- ds %>% mutate(in_movie = "yes") *ds <- ds %>% mutate(height_m = height/100, * bmi = mass/(height_m^2), * bmi = round(bmi)) ds ``` ] .panel2-mutate-auto[ ``` # A tibble: 87 × 7 name mass height hair_color in_movie height_m bmi <chr> <dbl> <int> <chr> <chr> <dbl> <dbl> 1 Luke Skywalker 77 172 blond yes 1.72 26 2 C-3PO 75 167 <NA> yes 1.67 27 3 R2-D2 32 96 <NA> yes 0.96 35 4 Darth Vader 136 202 none yes 2.02 33 5 Leia Organa 49 150 brown yes 1.5 22 6 Owen Lars 120 178 brown, grey yes 1.78 38 7 Beru Whitesun lars 75 165 brown yes 1.65 28 8 R5-D4 32 97 <NA> yes 0.97 34 9 Biggs Darklighter 84 183 black yes 1.83 25 10 Obi-Wan Kenobi 77 182 auburn, white yes 1.82 23 # … with 77 more rows ``` ] --- count: false # Mutate (add or modify columns) .panel1-mutate-auto[ ```r ds <- starwars %>% select(name, mass, height, hair_color) ds <- ds %>% mutate(in_movie = "yes") ds <- ds %>% mutate(height_m = height/100, bmi = mass/(height_m^2), bmi = round(bmi)) %>% * arrange(desc(bmi)) ds ``` ] .panel2-mutate-auto[ ``` # A tibble: 87 × 7 name mass height hair_color in_movie height_m bmi <chr> <dbl> <int> <chr> <chr> <dbl> <dbl> 1 Jabba Desilijic Tiure 1358 175 <NA> yes 1.75 443 2 Dud Bolt 45 94 none yes 0.94 51 3 Yoda 17 66 white yes 0.66 39 4 Owen Lars 120 178 brown, grey yes 1.78 38 5 R2-D2 32 96 <NA> yes 0.96 35 6 IG-88 140 200 none yes 2 35 7 R5-D4 32 97 <NA> yes 0.97 34 8 Jek Tono Porkins 110 180 brown yes 1.8 34 9 Grievous 159 216 none yes 2.16 34 10 Darth Vader 136 202 none yes 2.02 33 # … with 77 more rows ``` ] --- count: false # Mutate (add or modify columns) .panel1-mutate-auto[ ```r ds <- starwars %>% select(name, mass, height, hair_color) ds <- ds %>% mutate(in_movie = "yes") ds <- ds %>% mutate(height_m = height/100, bmi = mass/(height_m^2), bmi = round(bmi)) %>% arrange(desc(bmi)) *ds <- ds ds ``` ] .panel2-mutate-auto[ ``` # A tibble: 87 × 7 name mass height hair_color in_movie height_m bmi <chr> <dbl> <int> <chr> <chr> <dbl> <dbl> 1 Jabba Desilijic Tiure 1358 175 <NA> yes 1.75 443 2 Dud Bolt 45 94 none yes 0.94 51 3 Yoda 17 66 white yes 0.66 39 4 Owen Lars 120 178 brown, grey yes 1.78 38 5 R2-D2 32 96 <NA> yes 0.96 35 6 IG-88 140 200 none yes 2 35 7 R5-D4 32 97 <NA> yes 0.97 34 8 Jek Tono Porkins 110 180 brown yes 1.8 34 9 Grievous 159 216 none yes 2.16 34 10 Darth Vader 136 202 none yes 2.02 33 # … with 77 more rows ``` ] --- count: false # Mutate (add or modify columns) .panel1-mutate-auto[ ```r ds <- starwars %>% select(name, mass, height, hair_color) ds <- ds %>% mutate(in_movie = "yes") ds <- ds %>% mutate(height_m = height/100, bmi = mass/(height_m^2), bmi = round(bmi)) %>% arrange(desc(bmi)) ds <- ds %>% * filter(hair_color %in% c("blond", NA)) ds ``` ] .panel2-mutate-auto[ ``` # A tibble: 8 × 7 name mass height hair_color in_movie height_m bmi <chr> <dbl> <int> <chr> <chr> <dbl> <dbl> 1 Jabba Desilijic Tiure 1358 175 <NA> yes 1.75 443 2 R2-D2 32 96 <NA> yes 0.96 35 3 R5-D4 32 97 <NA> yes 0.97 34 4 C-3PO 75 167 <NA> yes 1.67 27 5 Luke Skywalker 77 172 blond yes 1.72 26 6 Greedo 74 173 <NA> yes 1.73 25 7 Anakin Skywalker 84 188 blond yes 1.88 24 8 Finis Valorum NA 170 blond yes 1.7 NA ``` ] --- count: false # Mutate (add or modify columns) .panel1-mutate-auto[ ```r ds <- starwars %>% select(name, mass, height, hair_color) ds <- ds %>% mutate(in_movie = "yes") ds <- ds %>% mutate(height_m = height/100, bmi = mass/(height_m^2), bmi = round(bmi)) %>% arrange(desc(bmi)) ds <- ds %>% filter(hair_color %in% c("blond", NA)) %>% * mutate(hair_color = ifelse(is.na(hair_color),"no hair", hair_color), * mass = ifelse(mass > 1000, "huge", "not huge")) ds ``` ] .panel2-mutate-auto[ ``` # A tibble: 8 × 7 name mass height hair_color in_movie height_m bmi <chr> <chr> <int> <chr> <chr> <dbl> <dbl> 1 Jabba Desilijic Tiure huge 175 no hair yes 1.75 443 2 R2-D2 not huge 96 no hair yes 0.96 35 3 R5-D4 not huge 97 no hair yes 0.97 34 4 C-3PO not huge 167 no hair yes 1.67 27 5 Luke Skywalker not huge 172 blond yes 1.72 26 6 Greedo not huge 173 no hair yes 1.73 25 7 Anakin Skywalker not huge 188 blond yes 1.88 24 8 Finis Valorum <NA> 170 blond yes 1.7 NA ``` ] <style> .panel1-mutate-auto { color: black; width: 81.6666666666667%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel2-mutate-auto { color: black; width: 16.3333333333333%; hight: 32%; float: none; padding-left: 1%; font-size: 80% } .panel3-mutate-auto { color: black; width: NA%; hight: 33%; float: none; padding-left: 1%; font-size: 80% } </style> --- # Tricky, common task: Changing some but not all values within a column ```r # Change all NA values to "no hair", keep all others the same ds <- ds %>% mutate(hair_color = ifelse(is.na(hair_color),"no hair", hair_color)) # Set all heights greater than 100 to 100, keep all others the same ds <- ds %>% mutate(height = ifelse(height > 100,100, height)) ``` --- count: false # Summarize (calculate variables in aggregate) .panel1-summarize-auto[ ```r *ds <- starwars %>% select(name, mass, height, species) ``` ] .panel2-summarize-auto[ ] --- count: false # Summarize (calculate variables in aggregate) .panel1-summarize-auto[ ```r ds <- starwars %>% select(name, mass, height, species) *ds %>% summarize(min_height = min(height)) ``` ] .panel2-summarize-auto[ ``` # A tibble: 1 × 1 min_height <int> 1 NA ``` ] --- count: false # Summarize (calculate variables in aggregate) .panel1-summarize-auto[ ```r ds <- starwars %>% select(name, mass, height, species) ds %>% summarize(min_height = min(height)) *ds %>% summarize(min_height = min(height, na.rm = T)) ``` ] .panel2-summarize-auto[ ``` # A tibble: 1 × 1 min_height <int> 1 NA ``` ``` # A tibble: 1 × 1 min_height <int> 1 66 ``` ] --- count: false # Summarize (calculate variables in aggregate) .panel1-summarize-auto[ ```r ds <- starwars %>% select(name, mass, height, species) ds %>% summarize(min_height = min(height)) ds %>% summarize(min_height = min(height, na.rm = T)) *ds %>% summarize(min_height = min(height, na.rm = T), * m_height = mean(height, na.rm = T), * max_height = max(height, na.rm = T)) ``` ] .panel2-summarize-auto[ ``` # A tibble: 1 × 1 min_height <int> 1 NA ``` ``` # A tibble: 1 × 1 min_height <int> 1 66 ``` ``` # A tibble: 1 × 3 min_height m_height max_height <int> <dbl> <int> 1 66 174. 264 ``` ] --- count: false # Summarize (calculate variables in aggregate) .panel1-summarize-auto[ ```r ds <- starwars %>% select(name, mass, height, species) ds %>% summarize(min_height = min(height)) ds %>% summarize(min_height = min(height, na.rm = T)) ds %>% summarize(min_height = min(height, na.rm = T), m_height = mean(height, na.rm = T), max_height = max(height, na.rm = T)) *ds %>% group_by(species) ``` ] .panel2-summarize-auto[ ``` # A tibble: 1 × 1 min_height <int> 1 NA ``` ``` # A tibble: 1 × 1 min_height <int> 1 66 ``` ``` # A tibble: 1 × 3 min_height m_height max_height <int> <dbl> <int> 1 66 174. 264 ``` ``` # A tibble: 87 × 4 # Groups: species [38] name mass height species <chr> <dbl> <int> <chr> 1 Luke Skywalker 77 172 Human 2 C-3PO 75 167 Droid 3 R2-D2 32 96 Droid 4 Darth Vader 136 202 Human 5 Leia Organa 49 150 Human 6 Owen Lars 120 178 Human 7 Beru Whitesun lars 75 165 Human 8 R5-D4 32 97 Droid 9 Biggs Darklighter 84 183 Human 10 Obi-Wan Kenobi 77 182 Human # … with 77 more rows ``` ] --- count: false # Summarize (calculate variables in aggregate) .panel1-summarize-auto[ ```r ds <- starwars %>% select(name, mass, height, species) ds %>% summarize(min_height = min(height)) ds %>% summarize(min_height = min(height, na.rm = T)) ds %>% summarize(min_height = min(height, na.rm = T), m_height = mean(height, na.rm = T), max_height = max(height, na.rm = T)) ds %>% group_by(species) %>% * summarize(min_height = min(height, na.rm = T), * m_height = mean(height, na.rm = T), * max_height = max(height, na.rm = T), * n = n()) ``` ] .panel2-summarize-auto[ ``` # A tibble: 1 × 1 min_height <int> 1 NA ``` ``` # A tibble: 1 × 1 min_height <int> 1 66 ``` ``` # A tibble: 1 × 3 min_height m_height max_height <int> <dbl> <int> 1 66 174. 264 ``` ``` # A tibble: 38 × 5 species min_height m_height max_height n <chr> <int> <dbl> <int> <int> 1 Aleena 79 79 79 1 2 Besalisk 198 198 198 1 3 Cerean 198 198 198 1 4 Chagrian 196 196 196 1 5 Clawdite 168 168 168 1 6 Droid 96 131. 200 6 7 Dug 112 112 112 1 8 Ewok 88 88 88 1 9 Geonosian 183 183 183 1 10 Gungan 196 209. 224 3 # … with 28 more rows ``` ] <style> .panel1-summarize-auto { color: black; width: 53.4545454545454%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-summarize-auto { color: black; width: 44.5454545454545%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-summarize-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> <!-- adjust font size and other formatting defs in this css code chunk --> <style type="text/css"> .remark-code{line-height: 1; font-size: 70%} @media print { .has-continuation { display: block; } } code.r.hljs.remark-code{ position: relative; overflow-x: hidden; } .remark-slide-number { opacity: 0; /* default: 0.5 */ } .content-box { box-sizing: border-box; background-color: #e2e2e2; } .content-box-blue, .content-box-gray, .content-box-grey, .content-box-army, .content-box-green, .content-box-purple, .content-box-red, .content-box-yellow { box-sizing: border-box; border-radius: 15px; margin: 0 0 15px; overflow: hidden; padding: 0px 20px 0px 20px; width: 100%; } .content-box-blue { background-color: #F0F8FF; } .content-box-gray { background-color: #e2e2e2; } .content-box-grey { background-color: #F5F5F5; } .content-box-army { background-color: #737a36; } .content-box-green { background-color: #d9edc2; } .content-box-purple { background-color: #e2e2f9; } .content-box-red { background-color: #ffcccc; } .content-box-yellow { background-color: #fef5c4; } .content-box-blue .remark-inline-code, .content-box-blue .remark-inline-code, .content-box-gray .remark-inline-code, .content-box-grey .remark-inline-code, .content-box-army .remark-inline-code, .content-box-green .remark-inline-code, .content-box-purple .remark-inline-code, .content-box-red .remark-inline-code, .content-box-yellow .remark-inline-code { background: none; } .scroll-box-8 { height:8em; overflow-y: scroll; } .scroll-box-10 { height:10em; overflow-y: scroll; } .scroll-box-12 { height:12em; overflow-y: scroll; } .scroll-box-14 { height:14em; overflow-y: scroll; } .scroll-box-16 { height:16em; overflow-y: scroll; } .scroll-box-18 { height:18em; overflow-y: scroll; } .scroll-box-20 { height:20em; overflow-y: scroll; } .scroll-output { height: 90%; overflow-y: scroll; } } /************************ * Font size and colours ************************/ /* LaTeX style */ .Large , .Large .remark-code, .Large .remark-inline-code { font-size: 144%; } .large , .large .remark-code, .large .remark-inline-code { font-size: 120%; } .small , .small .remark-code, .small .remark-inline-code { font-size: 90%; } .footnotesize, .footnotesize .remark-code, .footnotesize .remark-inline-code { font-size: 80%; } .scriptsize , .scriptsize .remark-code, .scriptsize .remark-inline-code { font-size: 70%; } .tiny , .tiny .remark-code, .tiny .remark-inline-code { font-size: 60%; } /* or you can be more specific */ .font10 , .code10 .remark-code, .code10 .remark-inline-code{ font-size: 10%; } .font20 , .code20 .remark-code, .code20 .remark-inline-code{ font-size: 20%; } .font30 , .code30 .remark-code, .code30 .remark-inline-code{ font-size: 30%; } .font40 , .code40 .remark-code, .code40 .remark-inline-code{ font-size: 40%; } .font50 , .code50 .remark-code, .code50 .remark-inline-code{ font-size: 50%; } .font60 , .code60 .remark-code, .code60 .remark-inline-code{ font-size: 60%; } .font70 , .code70 .remark-code, .code70 .remark-inline-code{ font-size: 70%; } .font75 , .code75 .remark-code, .code75 .remark-inline-code{ font-size: 75%; } .font80 , .code80 .remark-code, .code80 .remark-inline-code{ font-size: 80%; } .font90 , .code90 .remark-code, .code90 .remark-inline-code{ font-size: 90%; } .font100, .code100 .remark-code, .code100 .remark-inline-code{ font-size: 100%; } .font110, .code110 .remark-code, .code110 .remark-inline-code{ font-size: 110%; } .font120, .code120 .remark-code, .code120 .remark-inline-code{ font-size: 120%; } .font130, .code130 .remark-code, .code130 .remark-inline-code{ font-size: 130%; } .font140, .code140 .remark-code, .code140 .remark-inline-code{ font-size: 140%; } .font150, .code150 .remark-code, .code150 .remark-inline-code{ font-size: 150%; } .font160, .code160 .remark-code, .code160 .remark-inline-code{ font-size: 160%; } .font170, .code170 .remark-code, .code170 .remark-inline-code{ font-size: 170%; } .font175, .code175 .remark-code, .code175 .remark-inline-code{ font-size: 175%; } .font180, .code180 .remark-code, .code180 .remark-inline-code{ font-size: 180%; } .font190, .code190 .remark-code, .code190 .remark-inline-code{ font-size: 190%; } .font200, .code200 .remark-code, .code200 .remark-inline-code{ font-size: 200%; } .brand-red { color: #e64626; } .brand-blue { color: #0148A4; } .brand-yellow { color: #FFB800; } .brand-charcoal {color: #424242; } .brand-gray {color: #F1F1F1;} .brand-grey {color: #F1F1F1;} .black { color: black; } .white { color: white; } .red { color: red; } .blue { color: blue; } .green { color: green; } .yellow { color: yellow; } .orange { color: orange; } .purple { color: purple; } .gray { color: gray; } .grey { color: gray; } .bold { font-weight: bold; } .bolder { font-weight: bolder; } </style>