https://images.unsplash.com/photo-1590605103416-230704277b05?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80 class: left, top, inverse background-image: url(https://images.unsplash.com/photo-1590605103416-230704277b05?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) background-size: cover # .large[Reshaping] ## Long <--> Wide ### Walk-throughs <br>with {flipbookr}<br>and {xaringan} <br> <br> <br> <br> <br> <br> <br> <br> #### .right[Gina Reynolds<br>Photo Credit: Courtney Cook] --- ```r library(flipbookr) knitr::opts_chunk$set(cache = F, comment = "") ``` --- name: longtowideandback ## Exercise: *spread data, gather data* Sometimes, we need to transform the shape of our data. The wide form of data is an efficient way to store data - especially time-series data. However, we often need data in a long for for data analysis, so shape transformations are important. --- ## Exercise: *long to wide* --- count: false .panel1-long_to_wide-auto[ ```r *library(gapminder) ``` ] .panel2-long_to_wide-auto[ ] --- count: false .panel1-long_to_wide-auto[ ```r library(gapminder) *library(tidyverse) ``` ] .panel2-long_to_wide-auto[ ] --- count: false .panel1-long_to_wide-auto[ ```r library(gapminder) library(tidyverse) *gapminder ``` ] .panel2-long_to_wide-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-long_to_wide-auto[ ```r library(gapminder) library(tidyverse) gapminder %>% * select(country, continent, * lifeExp, year) ``` ] .panel2-long_to_wide-auto[ ``` # A tibble: 1,704 x 4 country continent lifeExp year <fct> <fct> <dbl> <int> 1 Afghanistan Asia 28.8 1952 2 Afghanistan Asia 30.3 1957 3 Afghanistan Asia 32.0 1962 4 Afghanistan Asia 34.0 1967 5 Afghanistan Asia 36.1 1972 6 Afghanistan Asia 38.4 1977 7 Afghanistan Asia 39.9 1982 8 Afghanistan Asia 40.8 1987 9 Afghanistan Asia 41.7 1992 10 Afghanistan Asia 41.8 1997 # … with 1,694 more rows ``` ] --- count: false .panel1-long_to_wide-auto[ ```r library(gapminder) library(tidyverse) gapminder %>% select(country, continent, lifeExp, year) %>% * pivot_wider(names_from = year, * values_from = lifeExp) ``` ] .panel2-long_to_wide-auto[ ``` # A tibble: 142 x 14 country continent `1952` `1957` `1962` `1967` `1972` `1977` `1982` `1987` <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 Afghan… Asia 28.8 30.3 32.0 34.0 36.1 38.4 39.9 40.8 2 Albania Europe 55.2 59.3 64.8 66.2 67.7 68.9 70.4 72 3 Algeria Africa 43.1 45.7 48.3 51.4 54.5 58.0 61.4 65.8 4 Angola Africa 30.0 32.0 34 36.0 37.9 39.5 39.9 39.9 5 Argent… Americas 62.5 64.4 65.1 65.6 67.1 68.5 69.9 70.8 6 Austra… Oceania 69.1 70.3 70.9 71.1 71.9 73.5 74.7 76.3 7 Austria Europe 66.8 67.5 69.5 70.1 70.6 72.2 73.2 74.9 8 Bahrain Asia 50.9 53.8 56.9 59.9 63.3 65.6 69.1 70.8 9 Bangla… Asia 37.5 39.3 41.2 43.5 45.3 46.9 50.0 52.8 10 Belgium Europe 68 69.2 70.2 70.9 71.4 72.8 73.9 75.4 # … with 132 more rows, and 4 more variables: `1992` <dbl>, `1997` <dbl>, # `2002` <dbl>, `2007` <dbl> ``` ] --- count: false .panel1-long_to_wide-auto[ ```r library(gapminder) library(tidyverse) gapminder %>% select(country, continent, lifeExp, year) %>% pivot_wider(names_from = year, values_from = lifeExp) -> *gapminder_life_exp_wide ``` ] .panel2-long_to_wide-auto[ ] <style> .panel1-long_to_wide-auto { color: black; width: 48.5050505050505%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-long_to_wide-auto { color: black; width: 49.4949494949495%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-long_to_wide-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## Exercise: *wide to long* --- count: false .panel1-wide_to_long-auto[ ```r *gapminder_life_exp_wide ``` ] .panel2-wide_to_long-auto[ ``` # A tibble: 142 x 14 country continent `1952` `1957` `1962` `1967` `1972` `1977` `1982` `1987` <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 Afghan… Asia 28.8 30.3 32.0 34.0 36.1 38.4 39.9 40.8 2 Albania Europe 55.2 59.3 64.8 66.2 67.7 68.9 70.4 72 3 Algeria Africa 43.1 45.7 48.3 51.4 54.5 58.0 61.4 65.8 4 Angola Africa 30.0 32.0 34 36.0 37.9 39.5 39.9 39.9 5 Argent… Americas 62.5 64.4 65.1 65.6 67.1 68.5 69.9 70.8 6 Austra… Oceania 69.1 70.3 70.9 71.1 71.9 73.5 74.7 76.3 7 Austria Europe 66.8 67.5 69.5 70.1 70.6 72.2 73.2 74.9 8 Bahrain Asia 50.9 53.8 56.9 59.9 63.3 65.6 69.1 70.8 9 Bangla… Asia 37.5 39.3 41.2 43.5 45.3 46.9 50.0 52.8 10 Belgium Europe 68 69.2 70.2 70.9 71.4 72.8 73.9 75.4 # … with 132 more rows, and 4 more variables: `1992` <dbl>, `1997` <dbl>, # `2002` <dbl>, `2007` <dbl> ``` ] --- count: false .panel1-wide_to_long-auto[ ```r gapminder_life_exp_wide %>% * pivot_longer(`1952`:`2007`, # backticks needed because numeric # column names look like number values without them * names_to = "year", * values_to = "lifeExp") ``` ] .panel2-wide_to_long-auto[ ``` # A tibble: 1,704 x 4 country continent year lifeExp <fct> <fct> <chr> <dbl> 1 Afghanistan Asia 1952 28.8 2 Afghanistan Asia 1957 30.3 3 Afghanistan Asia 1962 32.0 4 Afghanistan Asia 1967 34.0 5 Afghanistan Asia 1972 36.1 6 Afghanistan Asia 1977 38.4 7 Afghanistan Asia 1982 39.9 8 Afghanistan Asia 1987 40.8 9 Afghanistan Asia 1992 41.7 10 Afghanistan Asia 1997 41.8 # … with 1,694 more rows ``` ] --- count: false .panel1-wide_to_long-auto[ ```r gapminder_life_exp_wide %>% pivot_longer(`1952`:`2007`, # backticks needed because numeric # column names look like number values without them names_to = "year", values_to = "lifeExp") %>% * mutate(year = as.numeric(year)) ``` ] .panel2-wide_to_long-auto[ ``` # A tibble: 1,704 x 4 country continent year lifeExp <fct> <fct> <dbl> <dbl> 1 Afghanistan Asia 1952 28.8 2 Afghanistan Asia 1957 30.3 3 Afghanistan Asia 1962 32.0 4 Afghanistan Asia 1967 34.0 5 Afghanistan Asia 1972 36.1 6 Afghanistan Asia 1977 38.4 7 Afghanistan Asia 1982 39.9 8 Afghanistan Asia 1987 40.8 9 Afghanistan Asia 1992 41.7 10 Afghanistan Asia 1997 41.8 # … with 1,694 more rows ``` ] --- count: false .panel1-wide_to_long-auto[ ```r gapminder_life_exp_wide %>% pivot_longer(`1952`:`2007`, # backticks needed because numeric # column names look like number values without them names_to = "year", values_to = "lifeExp") %>% mutate(year = as.numeric(year)) -> * gapminder_life_exp_long ``` ] .panel2-wide_to_long-auto[ ] <style> .panel1-wide_to_long-auto { color: black; width: 48.5050505050505%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-wide_to_long-auto { color: black; width: 49.4949494949495%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-wide_to_long-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>