Version: 12 June 2020

- arrow keys: move through slides
- f: toggle full-screen

Goals

  • You know what tidyverse is.
  • Basic knowledge on the pipe %>% operator.

Tidyverse

Tidyverse is a collection of R packages developed for data science

  • tidyverse was initiated (and is to a large part programmed) by Hadley Wickham.
  • It is continuously developed and heavily promoted by RStudio.
  • The core idea is to implement (more) functional programming into R.
  • All packages share an underlying design philosophy, grammar, and data structure.
  • Although tidyverse is loved by many, there are also critics to it worth reading.
  • Tidyverse can be installed through R (install.packages(“tidyverse”)).

Pipes: %>%

An Example:

%>% takes the return of a function and pipes it to the next function.

# Without pipes (nested option):
kable(round(describe(select(dat, "iq", "age")), 2))

# or a second, better, alternative:
newdat <- select(dat, "iq", "age")
newdat <- describe(newdat)
newdat <- round(newdat, 2)
kable(newdat)
# And with pipes it looks like:
dat %>%
  select("iq", "age") %>%
  describe() %>%
  round(2) %>%
  kable()

Task

  • Take the mtcars data set (included in base R).
  • Apply the describe() function from the psych library (make sure you have installed psych).
  • Round the values to the first decimal (Note: round() is your friend here).
  • Use pipes %>% to do this all.
  • … and don’t forget to activate the tidyverse library :-)
Please stop the video here! Continue after completing the task!

library(psych)
library(tidyverse)

mtcars %>%
  describe() %>%
  round(1)
vars n mean sd median trimmed mad min max range skew kurtosis se
mpg 1 32 20.1 6.0 19.2 19.7 5.4 10.4 33.9 23.5 0.6 -0.4 1.1
cyl 2 32 6.2 1.8 6.0 6.2 3.0 4.0 8.0 4.0 -0.2 -1.8 0.3
disp 3 32 230.7 123.9 196.3 222.5 140.5 71.1 472.0 400.9 0.4 -1.2 21.9
hp 4 32 146.7 68.6 123.0 141.2 77.1 52.0 335.0 283.0 0.7 -0.1 12.1
drat 5 32 3.6 0.5 3.7 3.6 0.7 2.8 4.9 2.2 0.3 -0.7 0.1
wt 6 32 3.2 1.0 3.3 3.2 0.8 1.5 5.4 3.9 0.4 0.0 0.2
qsec 7 32 17.8 1.8 17.7 17.8 1.4 14.5 22.9 8.4 0.4 0.3 0.3
vs 8 32 0.4 0.5 0.0 0.4 0.0 0.0 1.0 1.0 0.2 -2.0 0.1
am 9 32 0.4 0.5 0.0 0.4 0.0 0.0 1.0 1.0 0.4 -1.9 0.1
gear 10 32 3.7 0.7 4.0 3.6 1.5 3.0 5.0 2.0 0.5 -1.1 0.1
carb 11 32 2.8 1.6 2.0 2.7 1.5 1.0 8.0 7.0 1.1 1.3 0.3