├── .gitignore ├── LICENSE ├── README.md ├── gather-multiples ├── make-multiples-to-gather.R └── make-multiples-to-gather.md ├── gather ├── gather-gif │ ├── gather-01.png │ ├── gather-02.png │ ├── gather-03.png │ ├── gather-04.png │ ├── gather-05.png │ ├── gather-06.png │ ├── gather-07.png │ └── gather-08.png ├── gather.gif ├── gather.pptx ├── gather_arguments.png ├── gather_to_two.png ├── make-data-to-gather.R ├── make-data-to-gather.md └── make-gather-gif.R ├── pipe ├── README.md ├── css │ ├── my-fonts.css │ └── my-theme.css ├── index.Rmd ├── index.html ├── libs │ └── remark-css │ │ └── default.css └── rladylego-pipe.jpg ├── pivot ├── make-pivot_longer-gif.R ├── pivot_longer.R ├── pivot_longer.md ├── pivot_longer.pptx ├── pivot_longer │ ├── Slide02.png │ ├── Slide03.png │ ├── Slide04.png │ ├── Slide05.png │ ├── Slide06.png │ ├── Slide07.png │ ├── Slide08.png │ ├── Slide09.png │ ├── Slide10.png │ ├── Slide11.png │ ├── Slide12.png │ ├── Slide13.png │ ├── Slide14.png │ ├── Slide15.png │ ├── Slide16.png │ └── Slide17.png ├── pivot_longer_larger.gif ├── pivot_longer_multi.pptx ├── pivot_longer_multi │ ├── Slide1.png │ ├── Slide10.png │ ├── Slide11.png │ ├── Slide12.png │ ├── Slide13.png │ ├── Slide14.png │ ├── Slide15.png │ ├── Slide16.png │ ├── Slide17.png │ ├── Slide18.png │ ├── Slide19.png │ ├── Slide2.png │ ├── Slide20.png │ ├── Slide21.png │ ├── Slide22.png │ ├── Slide23.png │ ├── Slide24.png │ ├── Slide25.png │ ├── Slide26.png │ ├── Slide27.png │ ├── Slide28.png │ ├── Slide29.png │ ├── Slide3.png │ ├── Slide4.png │ ├── Slide5.png │ ├── Slide6.png │ ├── Slide7.png │ ├── Slide8.png │ └── Slide9.png ├── pivot_longer_smaller.gif ├── pivot_multiples.R ├── pivot_multiples.md ├── pivot_wider.R └── pivot_wider.md ├── spread ├── make-data-to-spread.R ├── make-data-to-spread.md ├── make-spread-gif.R ├── spread-gif │ ├── Slide3.png │ ├── Slide4.png │ ├── Slide5.png │ ├── Slide6.png │ ├── Slide7.png │ ├── Slide8.png │ └── Slide9.png ├── spread.gif ├── spread.pptx ├── spread_arguments.png └── spread_from_two.png └── teachthat.Rproj /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | **/.DS_Store 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CC BY-SA 4.0 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # teachthat -------------------------------------------------------------------------------- /gather-multiples/make-multiples-to-gather.R: -------------------------------------------------------------------------------- 1 | #' --- 2 | #' output: github_document 3 | #' --- 4 | 5 | library(tidyverse) 6 | 7 | juniors_multiple <- tribble( 8 | ~ "baker", ~"score_1", ~"score_2", ~"score_3", ~ "guess_1", ~"guess_2", ~"guess_3", 9 | "Emma", 1L, 0L, 1L, "cinnamon", "cloves", "nutmeg", 10 | "Harry", 1L, 1L, 1L, "cinnamon", "cardamom", "nutmeg", 11 | "Ruby", 1L, 0L, 1L, "cinnamon", "cumin", "nutmeg", 12 | "Zainab", 0L, NA, 0L, "cardamom", NA_character_, "cinnamon" 13 | ) 14 | #' So we have 3 scores and 3 guesses, one for each trial. 15 | #' It would be great scores in 1 column and guesses in another column. 16 | #' Then, we would have 3 rows for baker/trial. 17 | #' That is the goal! 18 | juniors_multiple 19 | 20 | #' First gather all 6 columns into one column called `var_value`. 21 | #' Now, this means that temporarily, we just made things less tidy. 22 | juniors_multiple %>% 23 | gather(key = "var_name", value = "var_value", score_1:guess_3) %>% 24 | # we don't need to do this, but it helps to see 25 | arrange(baker, var_name) 26 | 27 | #' We know we need to spread at some point, but we are not there yet. 28 | #' We can't spread by `var_name` because we'll just get the original data set back. 29 | #' We need to `separate` the `var_name` column first into two columns: 30 | #' `var` and `order`. 31 | juniors_multiple %>% 32 | gather(key = "var_name", value = "var_value", -baker) %>% 33 | separate(var_name, into = c("var", "order"), convert = TRUE) %>% 34 | # we don't need to do this, but it helps to see 35 | arrange(baker, var) 36 | 37 | #' Almost there! Now we can `spread` to `var_value` column into 2 new columns. 38 | #' One will hold the scores for each trial; the other will hold guesses. 39 | juniors_multiple %>% 40 | gather(key = "var_name", value = "var_value", -baker) %>% 41 | separate(var_name, into = c("score_or_guess", "order"), convert = TRUE) %>% 42 | spread(score_or_guess, var_value) 43 | 44 | #' Boo- all characters! Convert and name the new tidy data. 45 | (tidy_juniors <- juniors_multiple %>% 46 | gather(key = "var_name", value = "var_value", -baker) %>% 47 | separate(var_name, into = c("score_or_guess", "order"), convert = TRUE) %>% 48 | spread(score_or_guess, var_value, convert = TRUE)) 49 | 50 | #' Although it doesn't make sense here, 51 | #' note that we could have instead spread by the order column. 52 | #' This creates 3 new colums- one for each trial. 53 | juniors_multiple %>% 54 | gather(key = "var_name", value = "var_value", -baker) %>% 55 | separate(var_name, into = c("score_or_guess", "order"), convert = TRUE) %>% 56 | spread(order, var_value, convert = TRUE) 57 | 58 | #' It is annoying to have new column names that are numbers though. 59 | #' `spread` has an argument to fix that: `sep`! 60 | #' Again this isn't tidy, because we have strings and numbers in the same columns. 61 | juniors_multiple %>% 62 | gather(key = "var_name", value = "var_value", -baker) %>% 63 | separate(var_name, into = c("score_or_guess", "order"), convert = TRUE) %>% 64 | spread(order, var_value, convert = TRUE, sep = "_") 65 | -------------------------------------------------------------------------------- /gather-multiples/make-multiples-to-gather.md: -------------------------------------------------------------------------------- 1 | make-multiples-to-gather.R 2 | ================ 3 | alison 4 | 2019-03-28 5 | 6 | ``` r 7 | library(tidyverse) 8 | ``` 9 | 10 | ## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ── 11 | 12 | ## ✔ ggplot2 3.1.0.9000 ✔ purrr 0.3.2.9000 13 | ## ✔ tibble 2.1.1 ✔ dplyr 0.8.0.9010 14 | ## ✔ tidyr 0.8.3.9000 ✔ stringr 1.4.0 15 | ## ✔ readr 1.3.1 ✔ forcats 0.4.0 16 | 17 | ## Warning: package 'forcats' was built under R version 3.5.2 18 | 19 | ## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── 20 | ## ✖ dplyr::filter() masks stats::filter() 21 | ## ✖ dplyr::lag() masks stats::lag() 22 | 23 | ``` r 24 | juniors_multiple <- tribble( 25 | ~ "baker", ~"score_1", ~"score_2", ~"score_3", ~ "guess_1", ~"guess_2", ~"guess_3", 26 | "Emma", 1L, 0L, 1L, "cinnamon", "cloves", "nutmeg", 27 | "Harry", 1L, 1L, 1L, "cinnamon", "cardamom", "nutmeg", 28 | "Ruby", 1L, 0L, 1L, "cinnamon", "cumin", "nutmeg", 29 | "Zainab", 0L, NA, 0L, "cardamom", NA_character_, "cinnamon" 30 | ) 31 | ``` 32 | 33 | So we have 3 scores and 3 guesses, one for each trial. It would be great 34 | scores in 1 column and guesses in another column. Then, we would have 3 35 | rows for baker/trial. That is the goal\! 36 | 37 | ``` r 38 | juniors_multiple 39 | ``` 40 | 41 | ## # A tibble: 4 x 7 42 | ## baker score_1 score_2 score_3 guess_1 guess_2 guess_3 43 | ## 44 | ## 1 Emma 1 0 1 cinnamon cloves nutmeg 45 | ## 2 Harry 1 1 1 cinnamon cardamom nutmeg 46 | ## 3 Ruby 1 0 1 cinnamon cumin nutmeg 47 | ## 4 Zainab 0 NA 0 cardamom cinnamon 48 | 49 | First gather all 6 columns into one column called `var_value`. Now, this 50 | means that temporarily, we just made things less tidy. 51 | 52 | ``` r 53 | juniors_multiple %>% 54 | gather(key = "var_name", value = "var_value", score_1:guess_3) %>% 55 | # we don't need to do this, but it helps to see 56 | arrange(baker, var_name) 57 | ``` 58 | 59 | ## # A tibble: 24 x 3 60 | ## baker var_name var_value 61 | ## 62 | ## 1 Emma guess_1 cinnamon 63 | ## 2 Emma guess_2 cloves 64 | ## 3 Emma guess_3 nutmeg 65 | ## 4 Emma score_1 1 66 | ## 5 Emma score_2 0 67 | ## 6 Emma score_3 1 68 | ## 7 Harry guess_1 cinnamon 69 | ## 8 Harry guess_2 cardamom 70 | ## 9 Harry guess_3 nutmeg 71 | ## 10 Harry score_1 1 72 | ## # … with 14 more rows 73 | 74 | We know we need to spread at some point, but we are not there yet. We 75 | can’t spread by `var_name` because we’ll just get the original data set 76 | back. We need to `separate` the `var_name` column first into two 77 | columns: `var` and `order`. 78 | 79 | ``` r 80 | juniors_multiple %>% 81 | gather(key = "var_name", value = "var_value", -baker) %>% 82 | separate(var_name, into = c("var", "order"), convert = TRUE) %>% 83 | # we don't need to do this, but it helps to see 84 | arrange(baker, var) 85 | ``` 86 | 87 | ## # A tibble: 24 x 4 88 | ## baker var order var_value 89 | ## 90 | ## 1 Emma guess 1 cinnamon 91 | ## 2 Emma guess 2 cloves 92 | ## 3 Emma guess 3 nutmeg 93 | ## 4 Emma score 1 1 94 | ## 5 Emma score 2 0 95 | ## 6 Emma score 3 1 96 | ## 7 Harry guess 1 cinnamon 97 | ## 8 Harry guess 2 cardamom 98 | ## 9 Harry guess 3 nutmeg 99 | ## 10 Harry score 1 1 100 | ## # … with 14 more rows 101 | 102 | Almost there\! Now we can `spread` to `var_value` column into 2 new 103 | columns. One will hold the scores for each trial; the other will hold 104 | guesses. 105 | 106 | ``` r 107 | juniors_multiple %>% 108 | gather(key = "var_name", value = "var_value", -baker) %>% 109 | separate(var_name, into = c("score_or_guess", "order"), convert = TRUE) %>% 110 | spread(score_or_guess, var_value) 111 | ``` 112 | 113 | ## # A tibble: 12 x 4 114 | ## baker order guess score 115 | ## 116 | ## 1 Emma 1 cinnamon 1 117 | ## 2 Emma 2 cloves 0 118 | ## 3 Emma 3 nutmeg 1 119 | ## 4 Harry 1 cinnamon 1 120 | ## 5 Harry 2 cardamom 1 121 | ## 6 Harry 3 nutmeg 1 122 | ## 7 Ruby 1 cinnamon 1 123 | ## 8 Ruby 2 cumin 0 124 | ## 9 Ruby 3 nutmeg 1 125 | ## 10 Zainab 1 cardamom 0 126 | ## 11 Zainab 2 127 | ## 12 Zainab 3 cinnamon 0 128 | 129 | Boo- all characters\! Convert and name the new tidy data. 130 | 131 | ``` r 132 | (tidy_juniors <- juniors_multiple %>% 133 | gather(key = "var_name", value = "var_value", -baker) %>% 134 | separate(var_name, into = c("score_or_guess", "order"), convert = TRUE) %>% 135 | spread(score_or_guess, var_value, convert = TRUE)) 136 | ``` 137 | 138 | ## # A tibble: 12 x 4 139 | ## baker order guess score 140 | ## 141 | ## 1 Emma 1 cinnamon 1 142 | ## 2 Emma 2 cloves 0 143 | ## 3 Emma 3 nutmeg 1 144 | ## 4 Harry 1 cinnamon 1 145 | ## 5 Harry 2 cardamom 1 146 | ## 6 Harry 3 nutmeg 1 147 | ## 7 Ruby 1 cinnamon 1 148 | ## 8 Ruby 2 cumin 0 149 | ## 9 Ruby 3 nutmeg 1 150 | ## 10 Zainab 1 cardamom 0 151 | ## 11 Zainab 2 NA 152 | ## 12 Zainab 3 cinnamon 0 153 | 154 | Although it doesn’t make sense here, note that we could have instead 155 | spread by the order column. This creates 3 new colums- one for each 156 | trial. 157 | 158 | ``` r 159 | juniors_multiple %>% 160 | gather(key = "var_name", value = "var_value", -baker) %>% 161 | separate(var_name, into = c("score_or_guess", "order"), convert = TRUE) %>% 162 | spread(order, var_value, convert = TRUE) 163 | ``` 164 | 165 | ## # A tibble: 8 x 5 166 | ## baker score_or_guess `1` `2` `3` 167 | ## 168 | ## 1 Emma guess cinnamon cloves nutmeg 169 | ## 2 Emma score 1 0 1 170 | ## 3 Harry guess cinnamon cardamom nutmeg 171 | ## 4 Harry score 1 1 1 172 | ## 5 Ruby guess cinnamon cumin nutmeg 173 | ## 6 Ruby score 1 0 1 174 | ## 7 Zainab guess cardamom cinnamon 175 | ## 8 Zainab score 0 0 176 | 177 | It is annoying to have new column names that are numbers though. 178 | `spread` has an argument to fix that: `sep`\! Again this isn’t tidy, 179 | because we have strings and numbers in the same columns. 180 | 181 | ``` r 182 | juniors_multiple %>% 183 | gather(key = "var_name", value = "var_value", -baker) %>% 184 | separate(var_name, into = c("score_or_guess", "order"), convert = TRUE) %>% 185 | spread(order, var_value, convert = TRUE, sep = "_") 186 | ``` 187 | 188 | ## # A tibble: 8 x 5 189 | ## baker score_or_guess order_1 order_2 order_3 190 | ## 191 | ## 1 Emma guess cinnamon cloves nutmeg 192 | ## 2 Emma score 1 0 1 193 | ## 3 Harry guess cinnamon cardamom nutmeg 194 | ## 4 Harry score 1 1 1 195 | ## 5 Ruby guess cinnamon cumin nutmeg 196 | ## 6 Ruby score 1 0 1 197 | ## 7 Zainab guess cardamom cinnamon 198 | ## 8 Zainab score 0 0 199 | -------------------------------------------------------------------------------- /gather/gather-gif/gather-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather-gif/gather-01.png -------------------------------------------------------------------------------- /gather/gather-gif/gather-02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather-gif/gather-02.png -------------------------------------------------------------------------------- /gather/gather-gif/gather-03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather-gif/gather-03.png -------------------------------------------------------------------------------- /gather/gather-gif/gather-04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather-gif/gather-04.png -------------------------------------------------------------------------------- /gather/gather-gif/gather-05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather-gif/gather-05.png -------------------------------------------------------------------------------- /gather/gather-gif/gather-06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather-gif/gather-06.png -------------------------------------------------------------------------------- /gather/gather-gif/gather-07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather-gif/gather-07.png -------------------------------------------------------------------------------- /gather/gather-gif/gather-08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather-gif/gather-08.png -------------------------------------------------------------------------------- /gather/gather.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather.gif -------------------------------------------------------------------------------- /gather/gather.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather.pptx -------------------------------------------------------------------------------- /gather/gather_arguments.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather_arguments.png -------------------------------------------------------------------------------- /gather/gather_to_two.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/gather/gather_to_two.png -------------------------------------------------------------------------------- /gather/make-data-to-gather.R: -------------------------------------------------------------------------------- 1 | #' --- 2 | #' output: github_document 3 | #' --- 4 | 5 | library(tidyverse) 6 | 7 | juniors_untidy <- tribble( 8 | ~ "baker", ~"cinnamon_1", ~"cardamom_2", ~"nutmeg_3", 9 | "Emma", 1L, 0L, 1L, 10 | "Harry", 1L, 1L, 1L, 11 | "Ruby", 1L, 0L, 1L, 12 | "Zainab", 0L, NA, 0L 13 | ) 14 | juniors_untidy 15 | 16 | # way one: select three columns 17 | juniors_untidy %>% 18 | gather(key = "spice", value = "correct", cinnamon_1:nutmeg_3) 19 | 20 | # way two: "freeze" baker 21 | juniors_untidy %>% 22 | gather(key = "spice", value = "correct", -baker) 23 | 24 | # way two, this time saving as a new data object 25 | (juniors_tidy <- juniors_untidy %>% 26 | gather(key = "spice", value = "correct", -baker)) -------------------------------------------------------------------------------- /gather/make-data-to-gather.md: -------------------------------------------------------------------------------- 1 | make-data-to-gather.R 2 | ================ 3 | alison 4 | Mon Nov 5 14:43:23 2018 5 | 6 | ``` r 7 | library(tidyverse) 8 | ``` 9 | 10 | ## ── Attaching packages ────────────────────────────────────────────────────── tidyverse 1.2.1 ── 11 | 12 | ## ✔ ggplot2 3.1.0 ✔ purrr 0.2.5 13 | ## ✔ tibble 1.4.2 ✔ dplyr 0.7.6 14 | ## ✔ tidyr 0.8.1 ✔ stringr 1.3.1 15 | ## ✔ readr 1.1.1 ✔ forcats 0.3.0 16 | 17 | ## ── Conflicts ───────────────────────────────────────────────────────── tidyverse_conflicts() ── 18 | ## ✖ dplyr::filter() masks stats::filter() 19 | ## ✖ dplyr::lag() masks stats::lag() 20 | 21 | ``` r 22 | juniors_untidy <- tribble( 23 | ~ "baker", ~"cinnamon_1", ~"cardamom_2", ~"nutmeg_3", 24 | "Emma", 1L, 0L, 1L, 25 | "Harry", 1L, 1L, 1L, 26 | "Ruby", 1L, 0L, 1L, 27 | "Zainab", 0L, NA, 0L 28 | ) 29 | juniors_untidy 30 | ``` 31 | 32 | ## # A tibble: 4 x 4 33 | ## baker cinnamon_1 cardamom_2 nutmeg_3 34 | ## 35 | ## 1 Emma 1 0 1 36 | ## 2 Harry 1 1 1 37 | ## 3 Ruby 1 0 1 38 | ## 4 Zainab 0 NA 0 39 | 40 | ``` r 41 | # way one: select three columns 42 | juniors_untidy %>% 43 | gather(key = "spice", value = "correct", cinnamon_1:nutmeg_3) 44 | ``` 45 | 46 | ## # A tibble: 12 x 3 47 | ## baker spice correct 48 | ## 49 | ## 1 Emma cinnamon_1 1 50 | ## 2 Harry cinnamon_1 1 51 | ## 3 Ruby cinnamon_1 1 52 | ## 4 Zainab cinnamon_1 0 53 | ## 5 Emma cardamom_2 0 54 | ## 6 Harry cardamom_2 1 55 | ## 7 Ruby cardamom_2 0 56 | ## 8 Zainab cardamom_2 NA 57 | ## 9 Emma nutmeg_3 1 58 | ## 10 Harry nutmeg_3 1 59 | ## 11 Ruby nutmeg_3 1 60 | ## 12 Zainab nutmeg_3 0 61 | 62 | ``` r 63 | # way two: "freeze" baker 64 | juniors_untidy %>% 65 | gather(key = "spice", value = "correct", -baker) 66 | ``` 67 | 68 | ## # A tibble: 12 x 3 69 | ## baker spice correct 70 | ## 71 | ## 1 Emma cinnamon_1 1 72 | ## 2 Harry cinnamon_1 1 73 | ## 3 Ruby cinnamon_1 1 74 | ## 4 Zainab cinnamon_1 0 75 | ## 5 Emma cardamom_2 0 76 | ## 6 Harry cardamom_2 1 77 | ## 7 Ruby cardamom_2 0 78 | ## 8 Zainab cardamom_2 NA 79 | ## 9 Emma nutmeg_3 1 80 | ## 10 Harry nutmeg_3 1 81 | ## 11 Ruby nutmeg_3 1 82 | ## 12 Zainab nutmeg_3 0 83 | 84 | ``` r 85 | # way two, this time saving as a new data object 86 | (juniors_tidy <- juniors_untidy %>% 87 | gather(key = "spice", value = "correct", -baker)) 88 | ``` 89 | 90 | ## # A tibble: 12 x 3 91 | ## baker spice correct 92 | ## 93 | ## 1 Emma cinnamon_1 1 94 | ## 2 Harry cinnamon_1 1 95 | ## 3 Ruby cinnamon_1 1 96 | ## 4 Zainab cinnamon_1 0 97 | ## 5 Emma cardamom_2 0 98 | ## 6 Harry cardamom_2 1 99 | ## 7 Ruby cardamom_2 0 100 | ## 8 Zainab cardamom_2 NA 101 | ## 9 Emma nutmeg_3 1 102 | ## 10 Harry nutmeg_3 1 103 | ## 11 Ruby nutmeg_3 1 104 | ## 12 Zainab nutmeg_3 0 105 | -------------------------------------------------------------------------------- /gather/make-gather-gif.R: -------------------------------------------------------------------------------- 1 | library(fs) 2 | library(magick) 3 | library(tidyverse) 4 | 5 | list_png <- dir_ls(path = here::here("gather", "gather-gif"), 6 | glob = "*.png") 7 | 8 | list_png %>% 9 | map(image_read) %>% # reads each path file 10 | image_join() %>% # joins image 11 | image_scale("800") %>% 12 | image_animate(fps = .5) %>% # animates, can opt for number of loops 13 | image_write(here::here("gather", "gather.gif")) # write the gif to file 14 | -------------------------------------------------------------------------------- /pipe/README.md: -------------------------------------------------------------------------------- 1 | See [rendered slides here](https://teachthat.netlify.com/pipe/#1) 2 | -------------------------------------------------------------------------------- /pipe/css/my-fonts.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Lato:400); 2 | @import url(https://fonts.googleapis.com/css?family=Inconsolata:400,700); 3 | @import url('https://fonts.googleapis.com/css?family=Cabin'); 4 | @import url('https://fonts.googleapis.com/css?family=Cormorant+Garamond:700i'); 5 | 6 | 7 | @font-face{ 8 | font-family: 'Fira Code'; 9 | src: url('eot/FiraCode-Light.eot'); 10 | src: url('eot/FiraCode-Light.eot') format('embedded-opentype'), 11 | url('woff2/FiraCode-Light.woff2') format('woff2'), 12 | url('woff/FiraCode-Light.woff') format('woff'), 13 | url('ttf/FiraCode-Light.ttf') format('truetype'); 14 | font-weight: 300; 15 | font-style: normal; 16 | } 17 | 18 | @font-face{ 19 | font-family: 'Fira Code'; 20 | src: url('eot/FiraCode-Regular.eot'); 21 | src: url('eot/FiraCode-Regular.eot') format('embedded-opentype'), 22 | url('woff2/FiraCode-Regular.woff2') format('woff2'), 23 | url('woff/FiraCode-Regular.woff') format('woff'), 24 | url('ttf/FiraCode-Regular.ttf') format('truetype'); 25 | font-weight: 400; 26 | font-style: normal; 27 | } 28 | 29 | @font-face{ 30 | font-family: 'Fira Code'; 31 | src: url('eot/FiraCode-Medium.eot'); 32 | src: url('eot/FiraCode-Medium.eot') format('embedded-opentype'), 33 | url('woff2/FiraCode-Medium.woff2') format('woff2'), 34 | url('woff/FiraCode-Medium.woff') format('woff'), 35 | url('ttf/FiraCode-Medium.ttf') format('truetype'); 36 | font-weight: 500; 37 | font-style: normal; 38 | } 39 | 40 | @font-face{ 41 | font-family: 'Fira Code'; 42 | src: url('eot/FiraCode-Bold.eot'); 43 | src: url('eot/FiraCode-Bold.eot') format('embedded-opentype'), 44 | url('woff2/FiraCode-Bold.woff2') format('woff2'), 45 | url('woff/FiraCode-Bold.woff') format('woff'), 46 | url('ttf/FiraCode-Bold.ttf') format('truetype'); 47 | font-weight: 700; 48 | font-style: normal; 49 | } 50 | 51 | body { 52 | font-family: 'Cabin', sans-serif; 53 | } 54 | 55 | h1, h2, h3 { 56 | font-family: 'Lato', sans-serif; 57 | } 58 | 59 | .remark-code { 60 | font-family: 'Fira Code', 'Inconsolata', 'Lucida Console', Monaco, monospace; 61 | } 62 | 63 | .remark-inline-code { 64 | font-family: 'Fira Code', 'Inconsolata', 'Lucida Console', Monaco, monospace; 65 | } 66 | 67 | @media print { 68 | body { 69 | font-family: 'Cabin', sans-serif; 70 | } 71 | 72 | 73 | .title-slide h2 { 74 | font-family: 'Cormorant Garamond', serif; 75 | font-style: italic; 76 | font-weight: 700; 77 | } 78 | 79 | .title-slide h3 { 80 | font-family: 'Cabin', sans-serif; 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /pipe/css/my-theme.css: -------------------------------------------------------------------------------- 1 | body { 2 | color: #585E60; 3 | } 4 | 5 | h1, h2, h3 { 6 | font-weight: normal; 7 | color: #126180; 8 | } 9 | 10 | .shout { color: #ee5863; 11 | font-family: 'Cormorant Garamond', serif; 12 | font-style: italic; 13 | font-weight: bold; 14 | font-size: larger; 15 | } 16 | 17 | .whisper { color: #84d6d3; 18 | font-family: 'Cormorant Garamond', serif; 19 | font-style: italic; 20 | font-weight: bold; 21 | font-size: larger; 22 | } 23 | 24 | .onfire { color: #ff7436; } 25 | 26 | /* link color */ 27 | a, a > code { 28 | color: #84d6d3; 29 | } 30 | 31 | /* unvisited link */ 32 | a:link { 33 | color: #84d6d3; 34 | } 35 | 36 | /* visited link */ 37 | a:visited { 38 | color: #84d6d3; 39 | } 40 | 41 | /* mouse over link */ 42 | a:hover { 43 | color: #ee5863; 44 | } 45 | 46 | /* selected link */ 47 | a:active { 48 | color: #ee5863; 49 | } 50 | 51 | .remark-code { 52 | color: #126180; 53 | } 54 | 55 | .remark-inline-code { 56 | color: #126180; 57 | } 58 | 59 | .remark-code-line-highlighted { 60 | background-color: #84d6d3; 61 | } 62 | 63 | .inverse { 64 | background-color: #126180; 65 | color: #FFFFFF; 66 | /*text-shadow: 0 0 20px #333;*/ 67 | } 68 | 69 | .inverse h1, .inverse h2, .inverse h3 { 70 | color: #FFFFFF; 71 | line-height: 1.0em; 72 | } 73 | 74 | .inverse .remark-code { 75 | color: #3498CC; 76 | } 77 | 78 | /* Two-column layout */ 79 | .left-column { 80 | color: #777; 81 | width: 25%; 82 | height: 92%; 83 | float: left; 84 | font-size: 18px; 85 | } 86 | .left-column h2:last-of-type, .left-column h3:last-child { 87 | color: #000; 88 | } 89 | .right-column { 90 | width: 75%; 91 | float: right; 92 | padding-top: 1em; 93 | } 94 | .pull-left { 95 | float: left; 96 | width: 47%; 97 | } 98 | .pull-right { 99 | float: right; 100 | width: 47%; 101 | } 102 | .pull-right ~ * { 103 | clear: both; 104 | } 105 | 106 | img, video, iframe { 107 | max-width: 100%; 108 | } 109 | blockquote { 110 | border-left: solid 5px lightgray; 111 | padding-left: 1em; 112 | } 113 | table { 114 | margin: auto; 115 | border-top: 1px solid #666; 116 | border-bottom: 1px solid #666; 117 | } 118 | table thead th { border-bottom: 1px solid #ddd; } 119 | th, td { padding: 5px; } 120 | thead, tfoot, tr:nth-child(even) { background: #eee } 121 | 122 | @page { margin: 0; } 123 | 124 | .title-slide { 125 | background-image: url("https://www.rstudio.com/wp-content/uploads/2014/04/magrittr.png"); 126 | background-position: 95% 95%; 127 | background-size: 250px; 128 | background-color: #126180; 129 | } 130 | 131 | .inverse h2 { 132 | line-height: 1.1; 133 | font-family: 'Cormorant Garamond', serif; 134 | font-style: italic; 135 | font-weight: 700; 136 | } 137 | 138 | .inverse .remark-inline-code { 139 | color: white; 140 | } 141 | 142 | .title-slide h1 { 143 | color: #FFFFFF; 144 | padding-top: 250px; 145 | font-size: 45px; 146 | line-height: 0.05; 147 | text-align: left; 148 | padding-bottom: 18px; 149 | margin-bottom: 18px; 150 | text-shadow: none; 151 | /*word-wrap: break-word;*/ 152 | } 153 | 154 | .title-slide h2 { 155 | color: #FFFFFF; 156 | font-size: 40px; 157 | text-align: left; 158 | padding-top: 0px; 159 | margin-top: 0px; 160 | text-shadow: none; 161 | } 162 | 163 | .title-slide h3 { 164 | font-family: 'Cabin', sans-serif; 165 | font-size: 25px; 166 | color: #FFFFFF; 167 | text-align: left; 168 | text-shadow: none; 169 | padding: 0px; 170 | margin: 0px; 171 | line-height: 1.1; 172 | } 173 | 174 | .title-slide .remark-slide-number { 175 | display: none; 176 | } 177 | 178 | 179 | 180 | 181 | -------------------------------------------------------------------------------- /pipe/index.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Using the `%>%` operator" 3 | subtitle: "Teaching with the tidyverse" 4 | output: 5 | xaringan::moon_reader: 6 | css: ["default", "css/my-theme.css", "css/my-fonts.css"] 7 | lib_dir: libs 8 | nature: 9 | highlightStyle: github 10 | highlightLines: true 11 | countIncrementalSlides: false 12 | --- 13 | 14 | ```{r setup, include=FALSE} 15 | options(htmltools.dir.version = FALSE) 16 | options(knitr.table.format = "html") 17 | knitr::opts_chunk$set(warning = FALSE, message = FALSE, 18 | comment = NA, dpi = 300, 19 | fig.align = "center", out.width = "65%", cache = FALSE, 20 | error = FALSE) 21 | library(tidyverse) 22 | library(babynames) 23 | library(emo) 24 | ``` 25 | 26 | 27 | class: middle, center, inverse 28 | 29 | ![](rladylego-pipe.jpg) 30 | 31 | ## What is the `%>%`? 32 | 33 | *image courtesy [@LegoRLady](https://twitter.com/LEGO_RLady/status/986661916855754752)* 34 | 35 | 36 | --- 37 | class: center, middle, inverse 38 | 39 | # `%>%` 40 | 41 | ## Combine multiple operations with the pipe 42 | 43 | https://r4ds.had.co.nz/transform.html#combining-multiple-operations-with-the-pipe 44 | 45 | --- 46 | class: center, middle, inverse 47 | 48 | # `%>%` 49 | 50 | ## The pipe 51 | 52 | *"dataframe first, dataframe once"* 53 | 54 | -- 55 | 56 | ```{r eval = FALSE} 57 | library(tidyverse) 58 | ``` 59 | 60 | 61 | -- 62 | 63 | RStudio Keyboard Shortcuts: 64 | 65 | OSX: `CMD` + `SHIFT` + `M` 66 | 67 | Else: `CTRL` + `SHIFT` + `M` 68 | 69 | --- 70 | class: middle 71 | 72 | .pull-left[ 73 | .shout[Nesting] a dataframe inside a function is .shout[hard to read]. 74 | 75 | ```{r} 76 | slice(babynames, 1) 77 | ``` 78 | 79 |
80 | 81 | 82 | 83 | Here, the "sentence" starts with a .shout[verb]. 84 | 85 | ] 86 | 87 | -- 88 | 89 | .pull-right[ 90 | 91 | .whisper[Piping] a dataframe into a function lets you read .whisper[L to R, top-to-bottom]. 92 | 93 | ```{r} 94 | babynames %>% slice(1) 95 | ``` 96 | 97 |
98 | 99 | 100 | Now, the "sentence" starts with a .whisper[noun]. 101 | 102 | ] 103 | 104 | 105 | 106 | --- 107 | class: middle 108 | 109 | .pull-left[ 110 | 111 | .shout[Sequences of functions] make you read .shout[inside out] 112 | 113 | ```{r} 114 | slice(filter(babynames, 115 | sex == "M"), 1) 116 | ``` 117 | 118 | ] 119 | 120 | -- 121 | 122 | .pull-right[ 123 | 124 | .whisper[Chaining functions] together lets you read .whisper[L to R, top-to-bottom] 125 | 126 | ```{r} 127 | babynames %>% 128 | filter(sex == "M") %>% 129 | slice(1) 130 | ``` 131 | 132 | ] 133 | 134 | --- 135 | ## Think .whisper["and then"] 136 | 137 | You can build up a series of pipes 138 | 139 | ```{r results='hide'} 140 | babynames %>% # dataframe first and then... #<< 141 | filter(sex == "M") %>% 142 | arrange(desc(year)) %>% 143 | slice(1) 144 | ``` 145 | 146 | --- 147 | ## Think .whisper["and then"] 148 | 149 | You can build up a series of pipes 150 | 151 | ```{r results='hide'} 152 | babynames %>% # dataframe first and then... #<< 153 | filter(sex == "M") %>% # filter only males and then...#<< 154 | arrange(desc(year)) %>% 155 | slice(1) 156 | ``` 157 | 158 | --- 159 | ## Think .whisper["and then"] 160 | 161 | You can build up a series of pipes 162 | 163 | ```{r results='hide'} 164 | babynames %>% # dataframe first and then... #<< 165 | filter(sex == "M") %>% # filter only males and then...#<< 166 | arrange(desc(year)) %>% # arrange by year and then...#<< 167 | slice(1) 168 | ``` 169 | 170 | 171 | --- 172 | ## Think .whisper["and then"] 173 | 174 | You can build up a series of pipes 175 | 176 | ```{r} 177 | babynames %>% # dataframe first and then... #<< 178 | filter(sex == "M") %>% # filter only males and then...#<< 179 | arrange(desc(year)) %>% # arrange by year and then...#<< 180 | slice(1) # slice the first row! #<< 181 | ``` 182 | 183 | --- 184 | ## Use assignment 185 | 186 | Using the `<-` assigns this new object a name. We ony have to do this _once_ with the `%>%`! 187 | 188 | -- 189 | 190 | .pull-left[ 191 | .shout[Bad] 192 | ```{r} 193 | boys <- filter(babynames, 194 | sex == "M") 195 | boys_desc <- arrange(boys, 196 | desc(year)) 197 | boy_latest <- slice(boys_desc, 198 | 1) 199 | 200 | boy_latest 201 | ``` 202 | ] 203 | 204 | -- 205 | 206 | 207 | .pull-right[ 208 | .whisper[Good] 209 | ```{r} 210 | boy_latest <- #<< 211 | babynames %>% 212 | filter(sex == "M") %>% 213 | arrange(desc(year)) %>% 214 | slice(1) 215 | 216 | boy_latest #<< 217 | ``` 218 | ] 219 | 220 | --- 221 | class: inverse, middle, center 222 | 223 | ```{r out.width = '50%', echo = FALSE} 224 | knitr::include_graphics("https://www.rstudio.com/wp-content/uploads/2014/04/magrittr.png") 225 | ``` 226 | 227 | ## "dataframe first, dataframe once" 228 | 229 | --- 230 | class: middle 231 | ## What you don't see 232 | 233 | ```{r} 234 | babynames %>% 235 | filter(sex == "M") %>% #<< 236 | arrange(desc(year)) %>% #<< 237 | slice(1) #<< 238 | ``` 239 | 240 |
241 | 242 | -- 243 | 244 | .pull-left[ 245 | 246 | This does the same thing: 247 | 248 | ```{r} 249 | babynames %>% 250 | filter(.data = ., sex == "M") %>% #<< 251 | arrange(.data = ., desc(year)) %>% #<< 252 | slice(.data = ., 1) #<< 253 | ``` 254 | 255 | ] 256 | 257 | -- 258 | 259 | .pull-right[ 260 | 261 | So does this: 262 | 263 | ```{r} 264 | babynames %>% 265 | filter(., sex == "M") %>% #<< 266 | arrange(., desc(year)) %>% #<< 267 | slice(., 1) #<< 268 | ``` 269 | 270 | ] 271 | 272 | --- 273 | class: inverse, middle, center 274 | 275 | ![](https://media.giphy.com/media/xUA7bfXlUyyE6FbOfu/giphy.gif) 276 | 277 | ## Why do I need to know about the dot (`.`)? 278 | 279 | --- 280 | ## Why care about `.`? 281 | 282 | For many non-`tidyverse` functions (and some `tidyverse` ones too), the dataframe is not _actually_ the first argument `r emo::ji("sad")`. 283 | 284 | -- 285 | 286 | 287 | .pull-left[ 288 | .shout[Bad] 289 | ```{r eval = FALSE} 290 | # none of these will work 291 | t.test(babynames, prop ~ sex) 292 | 293 | babynames %>% 294 | t.test(prop ~ sex) 295 | 296 | babynames %>% 297 | filter(year >= 2000) %>% 298 | t.test(prop ~ sex) 299 | ``` 300 | ] 301 | 302 | -- 303 | 304 | .pull-right[ 305 | .whisper[Good] 306 | ```{r eval = FALSE} 307 | # these will all work 308 | t.test(prop ~ sex, data = babynames) 309 | 310 | babynames %>% 311 | t.test(prop ~ sex, data = .) 312 | 313 | babynames %>% 314 | filter(year >= 2000) %>% 315 | t.test(prop ~ sex, data = .) 316 | ``` 317 | ] 318 | 319 | --- 320 | class:middle, inverse, center 321 | 322 | ![](https://media.giphy.com/media/TBddd797slSxO/giphy.gif) 323 | 324 | ## Using the `%>%` in tidyverse style 325 | 326 | --- 327 | 328 | From [The tidyverse style guide](https://style.tidyverse.org/pipes.html), the `%>%` should: 329 | 330 | -- 331 | 332 | - Have a space before, and a new line after. 333 | 334 | -- 335 | 336 | - Be indented by two spaces after the first line where you name the dataframe. 337 | 338 | -- 339 | 340 | - Include the `()` after each function name (even if you don't use arguments!) 341 | 342 | -- 343 | 344 | .pull-left[ 345 | .shout[Bad] 346 | ```{r eval = FALSE} 347 | babynames %>% 348 | filter(sex == "M") %>% slice(1) 349 | 350 | # with assignment 351 | boy_latest <- babynames %>% 352 | filter(sex == "M") %>% 353 | arrange(desc(year)) %>% 354 | slice(1) 355 | ``` 356 | ] 357 | 358 | -- 359 | 360 | .pull-right[ 361 | .whisper[Good] 362 | ```{r eval = FALSE} 363 | babynames %>% 364 | filter(sex == "M") %>% 365 | arrange(desc(year)) %>% 366 | slice(1) 367 | 368 | # with assignment 369 | boy_latest <- 370 | babynames %>% 371 | filter(sex == "M") %>% 372 | arrange(desc(year)) %>% 373 | slice(1) 374 | ``` 375 | ] 376 | 377 | 378 | -------------------------------------------------------------------------------- /pipe/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Using the %>% operator 5 | 6 | 7 | 8 | 9 | 10 | 11 | 459 | 460 | 474 | 475 | 493 | 494 | 504 | 505 | 506 | -------------------------------------------------------------------------------- /pipe/libs/remark-css/default.css: -------------------------------------------------------------------------------- 1 | a, a > code { 2 | color: rgb(249, 38, 114); 3 | text-decoration: none; 4 | } 5 | .footnote { 6 | position: absolute; 7 | bottom: 3em; 8 | padding-right: 4em; 9 | font-size: 90%; 10 | } 11 | .remark-code-line-highlighted { background-color: #ffff88; } 12 | 13 | .inverse { 14 | background-color: #272822; 15 | color: #d6d6d6; 16 | text-shadow: 0 0 20px #333; 17 | } 18 | .inverse h1, .inverse h2, .inverse h3 { 19 | color: #f3f3f3; 20 | } 21 | /* Two-column layout */ 22 | .left-column { 23 | color: #777; 24 | width: 20%; 25 | height: 92%; 26 | float: left; 27 | } 28 | .left-column h2:last-of-type, .left-column h3:last-child { 29 | color: #000; 30 | } 31 | .right-column { 32 | width: 75%; 33 | float: right; 34 | padding-top: 1em; 35 | } 36 | .pull-left { 37 | float: left; 38 | width: 47%; 39 | } 40 | .pull-right { 41 | float: right; 42 | width: 47%; 43 | } 44 | .pull-right ~ * { 45 | clear: both; 46 | } 47 | img, video, iframe { 48 | max-width: 100%; 49 | } 50 | blockquote { 51 | border-left: solid 5px lightgray; 52 | padding-left: 1em; 53 | } 54 | .remark-slide table { 55 | margin: auto; 56 | border-top: 1px solid #666; 57 | border-bottom: 1px solid #666; 58 | } 59 | .remark-slide table thead th { border-bottom: 1px solid #ddd; } 60 | th, td { padding: 5px; } 61 | .remark-slide thead, .remark-slide tfoot, .remark-slide tr:nth-child(even) { background: #eee } 62 | 63 | @page { margin: 0; } 64 | @media print { 65 | .remark-slide-scaler { 66 | width: 100% !important; 67 | height: 100% !important; 68 | transform: scale(1) !important; 69 | top: 0 !important; 70 | left: 0 !important; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /pipe/rladylego-pipe.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pipe/rladylego-pipe.jpg -------------------------------------------------------------------------------- /pivot/make-pivot_longer-gif.R: -------------------------------------------------------------------------------- 1 | library(fs) 2 | library(magick) 3 | library(tidyverse) 4 | 5 | list_png1 <- dir_ls(path = here::here("pivot", "pivot_longer"), 6 | glob = "*.png") %>% 7 | enframe() %>% 8 | mutate(slide_num = str_pad(parse_number(path_file(value)), width = 2, pad = "0"), 9 | new_path = glue::glue("{path_dir(value)}/Slide{slide_num}.png")) %>% 10 | arrange(slide_num) 11 | 12 | file_move(path = list_png1$value, new_path = list_png1$new_path) 13 | 14 | list_png <- dir_ls(path = here::here("pivot", "pivot_longer"), 15 | glob = "*.png") 16 | 17 | for_gif <- list_png %>% 18 | map(image_read) %>% # reads each path file 19 | image_join() # joins image 20 | 21 | 22 | for_gif %>% 23 | image_scale("800") %>% 24 | image_animate(fps = .5) %>% # animates, can opt for number of loops 25 | image_write(here::here("pivot", "pivot_longer_smaller.gif")) # write the gif to file 26 | 27 | for_gif %>% 28 | image_scale("2000") %>% 29 | image_animate(fps = .5) %>% # animates, can opt for number of loops 30 | image_write(here::here("pivot", "pivot_longer_larger.gif")) # write the gif to file 31 | -------------------------------------------------------------------------------- /pivot/pivot_longer.R: -------------------------------------------------------------------------------- 1 | #' --- 2 | #' output: github_document 3 | #' --- 4 | 5 | #' Use the development version of tidyr from GitHub: 6 | #+ devversion, eval = FALSE 7 | install.packages("devtools") 8 | devtools::install_github("tidyverse/tidyr") 9 | 10 | #+ packages, warning = FALSE, message = FALSE 11 | library(dplyr) 12 | library(tidyr) 13 | 14 | juniors_untidy <- tribble( 15 | ~ "baker", ~"cinnamon_1", ~"cardamom_2", ~"nutmeg_3", 16 | "Emma", 1L, 0L, 1L, 17 | "Harry", 1L, 1L, 1L, 18 | "Ruby", 1L, 0L, 1L, 19 | "Zainab", 0L, NA, 0L 20 | ) 21 | 22 | juniors_untidy %>% 23 | knitr::kable() 24 | 25 | #' way one: select three columns 26 | juniors_untidy %>% 27 | pivot_longer(cinnamon_1:nutmeg_3, 28 | names_to = "spice", 29 | values_to = "correct" 30 | ) 31 | 32 | #' way two: "freeze" baker 33 | juniors_untidy %>% 34 | pivot_longer(-baker, 35 | names_to = "spice", 36 | values_to = "correct" 37 | ) 38 | 39 | #' way two, this time saving as a new data object 40 | (juniors_tidy <- juniors_untidy %>% 41 | pivot_longer(-baker, 42 | names_to = "spice", 43 | values_to = "correct" 44 | ) %>% 45 | knitr::kable() 46 | ) 47 | 48 | #' split the current column names into two columns using names_to + names_sep 49 | juniors_untidy %>% 50 | pivot_longer(-baker, 51 | names_to = c("spice", "order"), 52 | names_sep = "_", 53 | values_to = "correct" 54 | ) %>% 55 | knitr::kable() -------------------------------------------------------------------------------- /pivot/pivot_longer.md: -------------------------------------------------------------------------------- 1 | pivot\_longer.R 2 | ================ 3 | alison 4 | 2019-03-29 5 | 6 | Use the development version of tidyr from GitHub: 7 | 8 | ``` r 9 | install.packages("devtools") 10 | devtools::install_github("tidyverse/tidyr") 11 | ``` 12 | 13 | ``` r 14 | library(dplyr) 15 | library(tidyr) 16 | 17 | juniors_untidy <- tribble( 18 | ~ "baker", ~"cinnamon_1", ~"cardamom_2", ~"nutmeg_3", 19 | "Emma", 1L, 0L, 1L, 20 | "Harry", 1L, 1L, 1L, 21 | "Ruby", 1L, 0L, 1L, 22 | "Zainab", 0L, NA, 0L 23 | ) 24 | 25 | juniors_untidy %>% 26 | knitr::kable() 27 | ``` 28 | 29 | | baker | cinnamon\_1 | cardamom\_2 | nutmeg\_3 | 30 | | :----- | ----------: | ----------: | --------: | 31 | | Emma | 1 | 0 | 1 | 32 | | Harry | 1 | 1 | 1 | 33 | | Ruby | 1 | 0 | 1 | 34 | | Zainab | 0 | NA | 0 | 35 | 36 | way one: select three columns 37 | 38 | ``` r 39 | juniors_untidy %>% 40 | pivot_longer(cinnamon_1:nutmeg_3, 41 | names_to = "spice", 42 | values_to = "correct" 43 | ) 44 | ``` 45 | 46 | ## # A tibble: 12 x 3 47 | ## baker spice correct 48 | ## 49 | ## 1 Emma cinnamon_1 1 50 | ## 2 Emma cardamom_2 0 51 | ## 3 Emma nutmeg_3 1 52 | ## 4 Harry cinnamon_1 1 53 | ## 5 Harry cardamom_2 1 54 | ## 6 Harry nutmeg_3 1 55 | ## 7 Ruby cinnamon_1 1 56 | ## 8 Ruby cardamom_2 0 57 | ## 9 Ruby nutmeg_3 1 58 | ## 10 Zainab cinnamon_1 0 59 | ## 11 Zainab cardamom_2 NA 60 | ## 12 Zainab nutmeg_3 0 61 | 62 | way two: “freeze” baker 63 | 64 | ``` r 65 | juniors_untidy %>% 66 | pivot_longer(-baker, 67 | names_to = "spice", 68 | values_to = "correct" 69 | ) 70 | ``` 71 | 72 | ## # A tibble: 12 x 3 73 | ## baker spice correct 74 | ## 75 | ## 1 Emma cinnamon_1 1 76 | ## 2 Emma cardamom_2 0 77 | ## 3 Emma nutmeg_3 1 78 | ## 4 Harry cinnamon_1 1 79 | ## 5 Harry cardamom_2 1 80 | ## 6 Harry nutmeg_3 1 81 | ## 7 Ruby cinnamon_1 1 82 | ## 8 Ruby cardamom_2 0 83 | ## 9 Ruby nutmeg_3 1 84 | ## 10 Zainab cinnamon_1 0 85 | ## 11 Zainab cardamom_2 NA 86 | ## 12 Zainab nutmeg_3 0 87 | 88 | way two, this time saving as a new data object 89 | 90 | ``` r 91 | (juniors_tidy <- juniors_untidy %>% 92 | pivot_longer(-baker, 93 | names_to = "spice", 94 | values_to = "correct" 95 | ) %>% 96 | knitr::kable() 97 | ) 98 | ``` 99 | 100 | | baker | spice | correct | 101 | | :----- | :---------- | ------: | 102 | | Emma | cinnamon\_1 | 1 | 103 | | Emma | cardamom\_2 | 0 | 104 | | Emma | nutmeg\_3 | 1 | 105 | | Harry | cinnamon\_1 | 1 | 106 | | Harry | cardamom\_2 | 1 | 107 | | Harry | nutmeg\_3 | 1 | 108 | | Ruby | cinnamon\_1 | 1 | 109 | | Ruby | cardamom\_2 | 0 | 110 | | Ruby | nutmeg\_3 | 1 | 111 | | Zainab | cinnamon\_1 | 0 | 112 | | Zainab | cardamom\_2 | NA | 113 | | Zainab | nutmeg\_3 | 0 | 114 | 115 | split the current column names into two columns using names\_to + 116 | names\_sep 117 | 118 | ``` r 119 | juniors_untidy %>% 120 | pivot_longer(-baker, 121 | names_to = c("spice", "order"), 122 | names_sep = "_", 123 | values_to = "correct" 124 | ) %>% 125 | knitr::kable() 126 | ``` 127 | 128 | | baker | spice | order | correct | 129 | | :----- | :------- | :---- | ------: | 130 | | Emma | cinnamon | 1 | 1 | 131 | | Emma | cardamom | 2 | 0 | 132 | | Emma | nutmeg | 3 | 1 | 133 | | Harry | cinnamon | 1 | 1 | 134 | | Harry | cardamom | 2 | 1 | 135 | | Harry | nutmeg | 3 | 1 | 136 | | Ruby | cinnamon | 1 | 1 | 137 | | Ruby | cardamom | 2 | 0 | 138 | | Ruby | nutmeg | 3 | 1 | 139 | | Zainab | cinnamon | 1 | 0 | 140 | | Zainab | cardamom | 2 | NA | 141 | | Zainab | nutmeg | 3 | 0 | 142 | -------------------------------------------------------------------------------- /pivot/pivot_longer.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer.pptx -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide02.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide03.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide04.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide05.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide06.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide07.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide08.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide09.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide09.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide10.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide11.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide12.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide13.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide14.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide15.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide16.png -------------------------------------------------------------------------------- /pivot/pivot_longer/Slide17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer/Slide17.png -------------------------------------------------------------------------------- /pivot/pivot_longer_larger.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_larger.gif -------------------------------------------------------------------------------- /pivot/pivot_longer_multi.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi.pptx -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide1.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide10.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide11.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide12.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide13.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide14.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide15.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide16.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide17.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide18.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide19.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide19.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide2.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide20.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide21.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide22.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide23.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide23.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide24.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide25.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide26.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide27.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide28.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide28.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide29.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide29.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide3.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide4.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide5.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide6.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide7.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide8.png -------------------------------------------------------------------------------- /pivot/pivot_longer_multi/Slide9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_multi/Slide9.png -------------------------------------------------------------------------------- /pivot/pivot_longer_smaller.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/pivot/pivot_longer_smaller.gif -------------------------------------------------------------------------------- /pivot/pivot_multiples.R: -------------------------------------------------------------------------------- 1 | #' --- 2 | #' output: github_document 3 | #' --- 4 | 5 | #' Use the development version of tidyr from GitHub: 6 | #+ devversion, eval = FALSE 7 | #install.packages("devtools") 8 | #devtools::install_github("tidyverse/tidyr") 9 | 10 | #+ packages, warning = FALSE, message = FALSE 11 | library(dplyr) 12 | library(tidyr) 13 | 14 | juniors_multiple <- tribble( 15 | ~ "baker", ~"score_1", ~"score_2", ~"score_3", ~ "guess_1", ~"guess_2", ~"guess_3", 16 | "Emma", 1L, 0L, 1L, "cinnamon", "cloves", "nutmeg", 17 | "Harry", 1L, 1L, 1L, "cinnamon", "cardamom", "nutmeg", 18 | "Ruby", 1L, 0L, 1L, "cinnamon", "cumin", "nutmeg", 19 | "Zainab", 0L, NA, 0L, "cardamom", NA_character_, "cinnamon" 20 | ) 21 | 22 | juniors_multiple %>% 23 | knitr::kable() 24 | 25 | #' I want three total columns: 26 | #' 27 | #' + first is order (1/2/3) --> these are the numbers at end of my column names 28 | #' + second is score (0/1/NA) 29 | #' + third is guess (cinnamon/cloves/nutmeg/NA) 30 | juniors_multiple %>% 31 | tidyr::pivot_longer(-baker, 32 | names_to = c(".value", "order"), 33 | names_sep = "_" 34 | ) 35 | 36 | #' order is a character 37 | #' make order a factor 38 | juniors_multiple %>% 39 | pivot_longer(-baker, 40 | names_to = c(".value", "order"), 41 | names_sep = "_", 42 | col_ptype = list( 43 | order = factor(levels = c(1, 2, 3)) 44 | ) 45 | ) 46 | 47 | #' make order a number instead 48 | juniors_multiple %>% 49 | pivot_longer(-baker, 50 | names_to = c(".value", "order"), 51 | names_sep = "_", 52 | col_ptype = list( 53 | order = integer() 54 | ) 55 | ) 56 | 57 | 58 | -------------------------------------------------------------------------------- /pivot/pivot_multiples.md: -------------------------------------------------------------------------------- 1 | pivot\_multiples.R 2 | ================ 3 | alison 4 | 2019-03-29 5 | 6 | Use the development version of tidyr from GitHub: 7 | 8 | ``` r 9 | #install.packages("devtools") 10 | #devtools::install_github("tidyverse/tidyr") 11 | ``` 12 | 13 | ``` r 14 | library(dplyr) 15 | library(tidyr) 16 | 17 | juniors_multiple <- tribble( 18 | ~ "baker", ~"score_1", ~"score_2", ~"score_3", ~ "guess_1", ~"guess_2", ~"guess_3", 19 | "Emma", 1L, 0L, 1L, "cinnamon", "cloves", "nutmeg", 20 | "Harry", 1L, 1L, 1L, "cinnamon", "cardamom", "nutmeg", 21 | "Ruby", 1L, 0L, 1L, "cinnamon", "cumin", "nutmeg", 22 | "Zainab", 0L, NA, 0L, "cardamom", NA_character_, "cinnamon" 23 | ) 24 | 25 | juniors_multiple %>% 26 | knitr::kable() 27 | ``` 28 | 29 | | baker | score\_1 | score\_2 | score\_3 | guess\_1 | guess\_2 | guess\_3 | 30 | | :----- | -------: | -------: | -------: | :------- | :------- | :------- | 31 | | Emma | 1 | 0 | 1 | cinnamon | cloves | nutmeg | 32 | | Harry | 1 | 1 | 1 | cinnamon | cardamom | nutmeg | 33 | | Ruby | 1 | 0 | 1 | cinnamon | cumin | nutmeg | 34 | | Zainab | 0 | NA | 0 | cardamom | NA | cinnamon | 35 | 36 | I want three total columns: 37 | 38 | - first is order (1/2/3) –\> these are the numbers at end of my column 39 | names 40 | - second is score (0/1/NA) 41 | - third is guess (cinnamon/cloves/nutmeg/NA) 42 | 43 | 44 | 45 | ``` r 46 | juniors_multiple %>% 47 | tidyr::pivot_longer(-baker, 48 | names_to = c(".value", "order"), 49 | names_sep = "_" 50 | ) 51 | ``` 52 | 53 | ## # A tibble: 12 x 4 54 | ## baker order score guess 55 | ## 56 | ## 1 Emma 1 1 cinnamon 57 | ## 2 Emma 2 0 cloves 58 | ## 3 Emma 3 1 nutmeg 59 | ## 4 Harry 1 1 cinnamon 60 | ## 5 Harry 2 1 cardamom 61 | ## 6 Harry 3 1 nutmeg 62 | ## 7 Ruby 1 1 cinnamon 63 | ## 8 Ruby 2 0 cumin 64 | ## 9 Ruby 3 1 nutmeg 65 | ## 10 Zainab 1 0 cardamom 66 | ## 11 Zainab 2 NA 67 | ## 12 Zainab 3 0 cinnamon 68 | 69 | order is a character make order a factor 70 | 71 | ``` r 72 | juniors_multiple %>% 73 | pivot_longer(-baker, 74 | names_to = c(".value", "order"), 75 | names_sep = "_", 76 | col_ptype = list( 77 | order = factor(levels = c(1, 2, 3)) 78 | ) 79 | ) 80 | ``` 81 | 82 | ## # A tibble: 12 x 4 83 | ## baker order score guess 84 | ## 85 | ## 1 Emma 1 1 cinnamon 86 | ## 2 Emma 2 0 cloves 87 | ## 3 Emma 3 1 nutmeg 88 | ## 4 Harry 1 1 cinnamon 89 | ## 5 Harry 2 1 cardamom 90 | ## 6 Harry 3 1 nutmeg 91 | ## 7 Ruby 1 1 cinnamon 92 | ## 8 Ruby 2 0 cumin 93 | ## 9 Ruby 3 1 nutmeg 94 | ## 10 Zainab 1 0 cardamom 95 | ## 11 Zainab 2 NA 96 | ## 12 Zainab 3 0 cinnamon 97 | 98 | make order a number instead 99 | 100 | ``` r 101 | juniors_multiple %>% 102 | pivot_longer(-baker, 103 | names_to = c(".value", "order"), 104 | names_sep = "_", 105 | col_ptype = list( 106 | order = integer() 107 | ) 108 | ) 109 | ``` 110 | 111 | ## # A tibble: 12 x 4 112 | ## baker order score guess 113 | ## 114 | ## 1 Emma 1 1 cinnamon 115 | ## 2 Emma 2 0 cloves 116 | ## 3 Emma 3 1 nutmeg 117 | ## 4 Harry 1 1 cinnamon 118 | ## 5 Harry 2 1 cardamom 119 | ## 6 Harry 3 1 nutmeg 120 | ## 7 Ruby 1 1 cinnamon 121 | ## 8 Ruby 2 0 cumin 122 | ## 9 Ruby 3 1 nutmeg 123 | ## 10 Zainab 1 0 cardamom 124 | ## 11 Zainab 2 NA 125 | ## 12 Zainab 3 0 cinnamon 126 | -------------------------------------------------------------------------------- /pivot/pivot_wider.R: -------------------------------------------------------------------------------- 1 | #' --- 2 | #' output: github_document 3 | #' --- 4 | 5 | #' Use the development version of tidyr from GitHub: 6 | #+ devversion, eval = FALSE 7 | # install.packages("devtools") 8 | devtools::install_github("tidyverse/tidyr") 9 | 10 | #+ packages, warning = FALSE, message = FALSE 11 | library(dplyr) 12 | library(tidyr) 13 | 14 | juniors_jumbled <- tibble::tribble( 15 | ~baker, ~var_name, ~var_value, 16 | "Emma", "age", "11", 17 | "Harry", "age", "10", 18 | "Ruby", "age", "11", 19 | "Zainab", "age", "10", 20 | "Emma", "outcome", "finalist", 21 | "Harry", "outcome", "winner", 22 | "Ruby", "outcome", "finalist", 23 | "Zainab", "outcome", "finalist", 24 | "Emma", "spices", "2", 25 | "Harry", "spices", "3", 26 | "Ruby", "spices", "2", 27 | "Zainab", "spices", "0" 28 | ) 29 | 30 | juniors_jumbled %>% 31 | knitr::kable() 32 | 33 | juniors_jumbled %>% 34 | pivot_wider(names_from = var_name, 35 | values_from = var_value) 36 | 37 | #' age and spices are characters, but we want numbers instead 38 | juniors_jumbled %>% 39 | pivot_wider(names_from = var_name, 40 | values_from = var_value) %>% 41 | mutate_at(vars(age, spices), as.integer) 42 | 43 | #' could do readr::type_convert on full tibble too 44 | juniors_jumbled %>% 45 | pivot_wider(names_from = var_name, 46 | values_from = var_value) %>% 47 | readr::type_convert() -------------------------------------------------------------------------------- /pivot/pivot_wider.md: -------------------------------------------------------------------------------- 1 | pivot\_wider.R 2 | ================ 3 | alison 4 | 2019-03-29 5 | 6 | Use the development version of tidyr from GitHub: 7 | 8 | ``` r 9 | # install.packages("devtools") 10 | devtools::install_github("tidyverse/tidyr") 11 | ``` 12 | 13 | ``` r 14 | library(dplyr) 15 | library(tidyr) 16 | 17 | juniors_jumbled <- tibble::tribble( 18 | ~baker, ~var_name, ~var_value, 19 | "Emma", "age", "11", 20 | "Harry", "age", "10", 21 | "Ruby", "age", "11", 22 | "Zainab", "age", "10", 23 | "Emma", "outcome", "finalist", 24 | "Harry", "outcome", "winner", 25 | "Ruby", "outcome", "finalist", 26 | "Zainab", "outcome", "finalist", 27 | "Emma", "spices", "2", 28 | "Harry", "spices", "3", 29 | "Ruby", "spices", "2", 30 | "Zainab", "spices", "0" 31 | ) 32 | 33 | juniors_jumbled %>% 34 | knitr::kable() 35 | ``` 36 | 37 | | baker | var\_name | var\_value | 38 | | :----- | :-------- | :--------- | 39 | | Emma | age | 11 | 40 | | Harry | age | 10 | 41 | | Ruby | age | 11 | 42 | | Zainab | age | 10 | 43 | | Emma | outcome | finalist | 44 | | Harry | outcome | winner | 45 | | Ruby | outcome | finalist | 46 | | Zainab | outcome | finalist | 47 | | Emma | spices | 2 | 48 | | Harry | spices | 3 | 49 | | Ruby | spices | 2 | 50 | | Zainab | spices | 0 | 51 | 52 | ``` r 53 | juniors_jumbled %>% 54 | pivot_wider(names_from = var_name, 55 | values_from = var_value) 56 | ``` 57 | 58 | ## # A tibble: 4 x 4 59 | ## baker age outcome spices 60 | ## 61 | ## 1 Emma 11 finalist 2 62 | ## 2 Harry 10 winner 3 63 | ## 3 Ruby 11 finalist 2 64 | ## 4 Zainab 10 finalist 0 65 | 66 | age and spices are characters, but we want numbers instead 67 | 68 | ``` r 69 | juniors_jumbled %>% 70 | pivot_wider(names_from = var_name, 71 | values_from = var_value) %>% 72 | mutate_at(vars(age, spices), as.integer) 73 | ``` 74 | 75 | ## # A tibble: 4 x 4 76 | ## baker age outcome spices 77 | ## 78 | ## 1 Emma 11 finalist 2 79 | ## 2 Harry 10 winner 3 80 | ## 3 Ruby 11 finalist 2 81 | ## 4 Zainab 10 finalist 0 82 | 83 | could do readr::type\_convert on full tibble too 84 | 85 | ``` r 86 | juniors_jumbled %>% 87 | pivot_wider(names_from = var_name, 88 | values_from = var_value) %>% 89 | readr::type_convert() 90 | ``` 91 | 92 | ## Parsed with column specification: 93 | ## cols( 94 | ## baker = col_character(), 95 | ## age = col_double(), 96 | ## outcome = col_character(), 97 | ## spices = col_double() 98 | ## ) 99 | 100 | ## # A tibble: 4 x 4 101 | ## baker age outcome spices 102 | ## 103 | ## 1 Emma 11 finalist 2 104 | ## 2 Harry 10 winner 3 105 | ## 3 Ruby 11 finalist 2 106 | ## 4 Zainab 10 finalist 0 107 | -------------------------------------------------------------------------------- /spread/make-data-to-spread.R: -------------------------------------------------------------------------------- 1 | #' --- 2 | #' output: github_document 3 | #' --- 4 | 5 | library(tidyverse) 6 | 7 | # we start tidy 8 | juniors_about <- tribble( 9 | ~ "baker", ~"age", ~"outcome", ~"spices", 10 | "Emma", 11L, "finalist", 2L, 11 | "Harry", 10L, "winner", 3L, 12 | "Ruby", 11L, "finalist", 2L, 13 | "Zainab", 10L, "finalist", 0L 14 | ) 15 | juniors_about 16 | 17 | # we spread to jumble (i.e., make untidy in specific way) 18 | (juniors_jumbled <- juniors_about %>% 19 | gather(key = "var_name", value = "var_value", -baker)) 20 | 21 | juniors_jumbled %>% 22 | knitr::kable() 23 | 24 | # spread to tidy- but all character variables! 25 | juniors_jumbled %>% 26 | spread(var_name, var_value) 27 | 28 | # spread to tidy + cast column types too- huzzah! 29 | juniors_jumbled %>% 30 | spread(var_name, var_value, convert = TRUE) 31 | -------------------------------------------------------------------------------- /spread/make-data-to-spread.md: -------------------------------------------------------------------------------- 1 | make-data-to-spread.R 2 | ================ 3 | alison 4 | 2019-03-28 5 | 6 | ``` r 7 | library(tidyverse) 8 | ``` 9 | 10 | ## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ── 11 | 12 | ## ✔ ggplot2 3.1.0.9000 ✔ purrr 0.3.2.9000 13 | ## ✔ tibble 2.1.1 ✔ dplyr 0.8.0.9010 14 | ## ✔ tidyr 0.8.3.9000 ✔ stringr 1.4.0 15 | ## ✔ readr 1.3.1 ✔ forcats 0.4.0 16 | 17 | ## Warning: package 'forcats' was built under R version 3.5.2 18 | 19 | ## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── 20 | ## ✖ dplyr::filter() masks stats::filter() 21 | ## ✖ dplyr::lag() masks stats::lag() 22 | 23 | ``` r 24 | # we start tidy 25 | juniors_about <- tribble( 26 | ~ "baker", ~"age", ~"outcome", ~"spices", 27 | "Emma", 11L, "finalist", 2L, 28 | "Harry", 10L, "winner", 3L, 29 | "Ruby", 11L, "finalist", 2L, 30 | "Zainab", 10L, "finalist", 0L 31 | ) 32 | juniors_about 33 | ``` 34 | 35 | ## # A tibble: 4 x 4 36 | ## baker age outcome spices 37 | ## 38 | ## 1 Emma 11 finalist 2 39 | ## 2 Harry 10 winner 3 40 | ## 3 Ruby 11 finalist 2 41 | ## 4 Zainab 10 finalist 0 42 | 43 | ``` r 44 | # we spread to jumble (i.e., make untidy in specific way) 45 | (juniors_jumbled <- juniors_about %>% 46 | gather(key = "var_name", value = "var_value", -baker)) 47 | ``` 48 | 49 | ## # A tibble: 12 x 3 50 | ## baker var_name var_value 51 | ## 52 | ## 1 Emma age 11 53 | ## 2 Harry age 10 54 | ## 3 Ruby age 11 55 | ## 4 Zainab age 10 56 | ## 5 Emma outcome finalist 57 | ## 6 Harry outcome winner 58 | ## 7 Ruby outcome finalist 59 | ## 8 Zainab outcome finalist 60 | ## 9 Emma spices 2 61 | ## 10 Harry spices 3 62 | ## 11 Ruby spices 2 63 | ## 12 Zainab spices 0 64 | 65 | ``` r 66 | juniors_jumbled %>% 67 | knitr::kable() 68 | ``` 69 | 70 | | baker | var\_name | var\_value | 71 | | :----- | :-------- | :--------- | 72 | | Emma | age | 11 | 73 | | Harry | age | 10 | 74 | | Ruby | age | 11 | 75 | | Zainab | age | 10 | 76 | | Emma | outcome | finalist | 77 | | Harry | outcome | winner | 78 | | Ruby | outcome | finalist | 79 | | Zainab | outcome | finalist | 80 | | Emma | spices | 2 | 81 | | Harry | spices | 3 | 82 | | Ruby | spices | 2 | 83 | | Zainab | spices | 0 | 84 | 85 | ``` r 86 | # spread to tidy- but all character variables! 87 | juniors_jumbled %>% 88 | spread(var_name, var_value) 89 | ``` 90 | 91 | ## # A tibble: 4 x 4 92 | ## baker age outcome spices 93 | ## 94 | ## 1 Emma 11 finalist 2 95 | ## 2 Harry 10 winner 3 96 | ## 3 Ruby 11 finalist 2 97 | ## 4 Zainab 10 finalist 0 98 | 99 | ``` r 100 | # spread to tidy + cast column types too- huzzah! 101 | juniors_jumbled %>% 102 | spread(var_name, var_value, convert = TRUE) 103 | ``` 104 | 105 | ## # A tibble: 4 x 4 106 | ## baker age outcome spices 107 | ## 108 | ## 1 Emma 11 finalist 2 109 | ## 2 Harry 10 winner 3 110 | ## 3 Ruby 11 finalist 2 111 | ## 4 Zainab 10 finalist 0 112 | -------------------------------------------------------------------------------- /spread/make-spread-gif.R: -------------------------------------------------------------------------------- 1 | library(fs) 2 | library(magick) 3 | library(tidyverse) 4 | 5 | list_png <- dir_ls(path = here::here("spread", "spread-gif"), 6 | glob = "*.png") 7 | 8 | list_png %>% 9 | map(image_read) %>% # reads each path file 10 | image_join() %>% # joins image 11 | image_scale("1000") %>% # twitter needs smaller 12 | image_animate(fps = .5) %>% # animates, can opt for number of loops 13 | image_write(here::here("spread", "spread.gif")) # write the gif to file 14 | -------------------------------------------------------------------------------- /spread/spread-gif/Slide3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread-gif/Slide3.png -------------------------------------------------------------------------------- /spread/spread-gif/Slide4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread-gif/Slide4.png -------------------------------------------------------------------------------- /spread/spread-gif/Slide5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread-gif/Slide5.png -------------------------------------------------------------------------------- /spread/spread-gif/Slide6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread-gif/Slide6.png -------------------------------------------------------------------------------- /spread/spread-gif/Slide7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread-gif/Slide7.png -------------------------------------------------------------------------------- /spread/spread-gif/Slide8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread-gif/Slide8.png -------------------------------------------------------------------------------- /spread/spread-gif/Slide9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread-gif/Slide9.png -------------------------------------------------------------------------------- /spread/spread.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread.gif -------------------------------------------------------------------------------- /spread/spread.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread.pptx -------------------------------------------------------------------------------- /spread/spread_arguments.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread_arguments.png -------------------------------------------------------------------------------- /spread/spread_from_two.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apreshill/teachthat/71b6a9297147dacca8ea5ef13e6473ce3bcde400/spread/spread_from_two.png -------------------------------------------------------------------------------- /teachthat.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: XeLaTeX 14 | --------------------------------------------------------------------------------