class: left, top, inverse background-image: url(https://images.unsplash.com/photo-1546198632-9ef6368bef12?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) background-size: cover # .large[logical indexing] ### Walk through <br>with {flipbookr}<br>and {xaringan} <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> #### .right[Gina Reynolds<br>Photo Credit: Denny Müller] --- # Data wrangling with dplyr and tidyr ```r library(magrittr) library(gapminder) ``` --- count: false .panel1-load_packages_manipulation-auto[ ```r *gm_2002 <- gapminder gm_2002 ``` ] .panel2-load_packages_manipulation-auto[ ``` # A tibble: 1,704 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 1952 28.8 8425333 779. 2 Afghanistan Asia 1957 30.3 9240934 821. 3 Afghanistan Asia 1962 32.0 10267083 853. 4 Afghanistan Asia 1967 34.0 11537966 836. 5 Afghanistan Asia 1972 36.1 13079460 740. 6 Afghanistan Asia 1977 38.4 14880372 786. 7 Afghanistan Asia 1982 39.9 12881816 978. 8 Afghanistan Asia 1987 40.8 13867957 852. 9 Afghanistan Asia 1992 41.7 16317921 649. 10 Afghanistan Asia 1997 41.8 22227415 635. # … with 1,694 more rows ``` ] --- count: false .panel1-load_packages_manipulation-auto[ ```r gm_2002 <- gapminder %>% * .[, c("year", "country", * "pop", "gdpPercap")] gm_2002 ``` ] .panel2-load_packages_manipulation-auto[ ``` # A tibble: 1,704 x 4 year country pop gdpPercap <int> <fct> <int> <dbl> 1 1952 Afghanistan 8425333 779. 2 1957 Afghanistan 9240934 821. 3 1962 Afghanistan 10267083 853. 4 1967 Afghanistan 11537966 836. 5 1972 Afghanistan 13079460 740. 6 1977 Afghanistan 14880372 786. 7 1982 Afghanistan 12881816 978. 8 1987 Afghanistan 13867957 852. 9 1992 Afghanistan 16317921 649. 10 1997 Afghanistan 22227415 635. # … with 1,694 more rows ``` ] --- count: false .panel1-load_packages_manipulation-auto[ ```r gm_2002 <- gapminder %>% .[, c("year", "country", "pop", "gdpPercap")] %>% * .[gapminder$year == 2002, ] gm_2002 ``` ] .panel2-load_packages_manipulation-auto[ ``` # A tibble: 142 x 4 year country pop gdpPercap <int> <fct> <int> <dbl> 1 2002 Afghanistan 25268405 727. 2 2002 Albania 3508512 4604. 3 2002 Algeria 31287142 5288. 4 2002 Angola 10866106 2773. 5 2002 Argentina 38331121 8798. 6 2002 Australia 19546792 30688. 7 2002 Austria 8148312 32418. 8 2002 Bahrain 656397 23404. 9 2002 Bangladesh 135656790 1136. 10 2002 Belgium 10311970 30486. # … with 132 more rows ``` ] --- count: false .panel1-load_packages_manipulation-auto[ ```r gm_2002 <- gapminder %>% .[, c("year", "country", "pop", "gdpPercap")] %>% .[gapminder$year == 2002, ] %>% * .[, -1] gm_2002 ``` ] .panel2-load_packages_manipulation-auto[ ``` # A tibble: 142 x 3 country pop gdpPercap <fct> <int> <dbl> 1 Afghanistan 25268405 727. 2 Albania 3508512 4604. 3 Algeria 31287142 5288. 4 Angola 10866106 2773. 5 Argentina 38331121 8798. 6 Australia 19546792 30688. 7 Austria 8148312 32418. 8 Bahrain 656397 23404. 9 Bangladesh 135656790 1136. 10 Belgium 10311970 30486. # … with 132 more rows ``` ] --- count: false .panel1-load_packages_manipulation-auto[ ```r gm_2002 <- gapminder %>% .[, c("year", "country", "pop", "gdpPercap")] %>% .[gapminder$year == 2002, ] %>% .[, -1] *gm_2002$gdp <- with(gm_2002, * pop * gdpPercap) gm_2002 ``` ] .panel2-load_packages_manipulation-auto[ ``` # A tibble: 142 x 4 country pop gdpPercap gdp <fct> <int> <dbl> <dbl> 1 Afghanistan 25268405 727. 18363410424. 2 Albania 3508512 4604. 16153932130. 3 Algeria 31287142 5288. 165447670333. 4 Angola 10866106 2773. 30134833901. 5 Argentina 38331121 8798. 337223430800. 6 Australia 19546792 30688. 599847158654. 7 Austria 8148312 32418. 264148781752. 8 Bahrain 656397 23404. 15362026094. 9 Bangladesh 135656790 1136. 154159077921. 10 Belgium 10311970 30486. 314369518653. # … with 132 more rows ``` ] <style> .panel1-load_packages_manipulation-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-load_packages_manipulation-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-load_packages_manipulation-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-free_form-auto[ ```r *my_data <- 18:1 my_data ``` ] .panel2-free_form-auto[ ``` [1] 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ``` ] --- count: false .panel1-free_form-auto[ ```r my_data <- 18:1 %>% * .[c(1,10,18)] my_data ``` ] .panel2-free_form-auto[ ``` [1] 18 9 1 ``` ] --- count: false .panel1-free_form-auto[ ```r my_data <- 18:1 %>% .[c(1,10,18)] %>% * data.frame(y = rep("hi", 3), * q = 1:3, * hi = 7:9) my_data ``` ] .panel2-free_form-auto[ ``` . y q hi 1 18 hi 1 7 2 9 hi 2 8 3 1 hi 3 9 ``` ] --- count: false .panel1-free_form-auto[ ```r my_data <- 18:1 %>% .[c(1,10,18)] %>% data.frame(y = rep("hi", 3), q = 1:3, hi = 7:9) %>% * .[2:3, ] my_data ``` ] .panel2-free_form-auto[ ``` . y q hi 2 9 hi 2 8 3 1 hi 3 9 ``` ] --- count: false .panel1-free_form-auto[ ```r my_data <- 18:1 %>% .[c(1,10,18)] %>% data.frame(y = rep("hi", 3), q = 1:3, hi = 7:9) %>% .[2:3, ] *names(my_data)[1] <- "var1" my_data ``` ] .panel2-free_form-auto[ ``` var1 y q hi 2 9 hi 2 8 3 1 hi 3 9 ``` ] <style> .panel1-free_form-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-free_form-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-free_form-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> <style type="text/css"> .remark-code{line-height: 1.5; font-size: 90%} @media print { .has-continuation { display: block; } } </style>