class: title-slide <br> <br> .right-panel[ # Introduction to tidyverse ## Mine Dogucu ] --- ## The tidyverse style guide >canyoureadthissentence? .important[ Good coding style is like correct punctuation: you can manage without it, but it makes things easier to read. ] - The most important thing about tidyverse style guide is that it provides consistency, making code easier to write because you need to make fewer decisions. --- File names - Avoid using special characters in file names - stick with numbers, letters, -, and _. ``` r # Good fit_models.R utility_functions.R # Bad fit models.R foo.r stuff.r ``` --- Syntax - Before and after operators (e.g. <-, =) leave spaces. - Put a space after a comma, not before. - Object names are all lower case, with words separated by an underscore. ``` r # Good age <- c(6, 9, 15) # Bad age <- c(6,9,15) # Good data.frame(age_kid = age) # Bad data.frame(Age_kid=age) # Good day_one <- 1 day_1 <- 1 # Bad DayOne <- 1 first_day_of_the_month <- 1 ``` -[The tidyverse style guide](https://style.tidyverse.org/) --- class: inverse middle .font50[The Pipe Operator] --- class: middle >Three solutions to a single problem What is the average of 4, 8, 16 approximately? --- class: middle 1.What is the average of **4, 8, 16** approximately? --- class: middle 2.What is the **average** of 4, 8, 16 approximately? --- class: middle 3.What is the average of 4, 8, 16 **approximately**? --- class: middle inverse .font50[Solution 1: Functions within Functions] --- ``` r c(4, 8, 16) ``` ``` ## [1] 4 8 16 ``` -- <hr> ``` r mean(c(4, 8, 16)) ``` ``` ## [1] 9.333333 ``` -- <hr> ``` r round(mean(c(4, 8, 16))) ``` ``` ## [1] 9 ``` --- class: middle **Problem with writing functions within functions** Things will get messy and more difficult to read and debug as we deal with more complex operations on data. --- class: middle inverse .font50[Solution 2: Creating Objects] --- class: middle ``` r numbers <- c(4, 8, 16) numbers ``` ``` ## [1] 4 8 16 ``` -- <hr> ``` r avg_number <- mean(numbers) avg_number ``` ``` ## [1] 9.333333 ``` -- <hr> ``` r round(avg_number) ``` ``` ## [1] 9 ``` --- class: middle **Problem with creating many objects** We will end up with too many objects in `Environment`. --- class: middle inverse .font50[Solution 3: The (forward) Pipe Operator %>% ] --- class: middle .font75[Shortcut: <br>Ctrl (Command) + Shift + M] --- class: middle Before using pipe, install package called 'tidyverse' Recall R package, how to use a function/operator from a package? --- class: middle ``` r #install.packages('tidyverse') library(tidyverse) ``` ``` ## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ── ## ✔ dplyr 1.1.4 ✔ readr 2.1.5 ## ✔ forcats 1.0.0 ✔ stringr 1.5.1 ## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1 ## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1 ## ✔ purrr 1.0.2 ## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ── ## ✖ tidyr::extract() masks magrittr::extract() ## ✖ dplyr::filter() masks stats::filter() ## ✖ dplyr::lag() masks stats::lag() ## ✖ purrr::set_names() masks magrittr::set_names() ## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors ``` --- class: middle .pull-left[ ``` r c(4, 8, 16) %>% mean() %>% round() ``` ``` ## [1] 9 ``` ] .pull-right[ Combine 4, 8, and 16 `and then` Take the mean `and then` Round the output ] --- class: middle The output of the first function is the first argument of the second function. --- Do you recall composite functions such as `\(f \circ g(x)\)`? -- Now we have `\(f \circ g \circ h (x)\)` or `round(mean(c(4, 8, 16)))` -- .pull-left[ ``` r h(x) %>% g() %>% f() ``` ] .pull-right[ ``` r c(4, 8, 16) %>% mean() %>% round() ``` ]