Review the tidyverse

We always load the tidyverse first

library("tidyverse")
library("DT")

Our example dataset is from starwars!

starwars %>%
  head() %>%
  datatable()

Calculate median height..

starwars %>%
  group_by(species) %>%
  summarize(median.height = median(height, 
                                   na.rm = TRUE)) %>%
  arrange(desc(median.height)) %>%
  knitr::kable()
species median.height
Quermian 264.0
Wookiee 231.0
Kaminoan 221.0
Kaleesh 216.0
Gungan 206.0
Pau’an 206.0
Besalisk 198.0
Cerean 198.0
Chagrian 196.0
Nautolan 196.0
Skakoan 193.0
Muun 191.0
Neimodian 191.0
Trandoshan 190.0
Iktotchi 188.0
Kel Dor 188.0
Tholothian 184.0
Geonosian 183.0
NA 180.5
Human 180.0
Mon Calamari 180.0
Twi’lek 179.0
Togruta 178.0
Hutt 175.0
Rodian 173.0
Zabrak 173.0
Clawdite 168.0
Mirialan 168.0
Toong 163.0
Sullustan 160.0
Toydarian 137.0
Droid 132.0
Xexto 122.0
Dug 112.0
Vulptereen 94.0
Ewok 88.0
Aleena 79.0
Yoda’s species 66.0

Making the tidyverse purrr

purrr gives us functional programming in R

There’s a tutorial here: https://jennybc.github.io/purrr-tutorial/

https://github.com/tidyverse/dplyr/blob/master/data-raw/starwars.R

Tidying up with the tidyverse

Tests give you ugly, unusable output

t.test(1:10, 3:13)
## 
##  Welch Two Sample t-test
## 
## data:  1:10 and 3:13
## t = -1.8058, df = 18.998, p-value = 0.08682
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -5.3976771  0.3976771
## sample estimates:
## mean of x mean of y 
##       5.5       8.0
t.test(1:10, 3:13) %>%
  broom::tidy()
## # A tibble: 1 x 10
##   estimate estimate1 estimate2 statistic p.value parameter conf.low
##      <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>
## 1     -2.5       5.5         8     -1.81  0.0868      19.0    -5.40
## # ... with 3 more variables: conf.high <dbl>, method <chr>,
## #   alternative <chr>

ggplot2 in RMarkdown

Here’s some bootstrap…