Tidyverse

Throughout this workshop you will come into contact with some functions from the very popular package collection tidyverse. The tidyverse is composed of multiple packages, all following a common philosophy, and facilitating many aspects of coding in R, for example data wrangling and plotting. It is not really necessary to learn the tidyverse syntax in order to understand R and become proficient in it. However, I find it easier to understand in many cases, which probably makes it easier to get started with. Therefore, I will provide the syntax from the respective tidyverse package along with the Base R syntax in many cases. In the end, it is a question of preference what you want to learn. Most code will probably be composed from base R functions and tidyverse functions.

# Tidyverse syntax will be marked like this.

We will mainly use the tidyverse packages dpylr and ggplot2.

The Pipe Operator

tidyverse code is often written using the pipe operator %>% (read as ‘then do’), which makes it easy to connect multiple function calls:

In base R, one could write:

sum(seq(from = 1, to = mean(c(45:100), na.rm = TRUE), by = 0.1))
[1] 26313

Which, in the tidyverse, would be written like so:

library(tidyverse)

c(45:100) %>%
  mean(na.rm = TRUE) %>%
  seq(from = 1, to = ., by = 0.1) %>%
  sum
[1] 26313

Much nicer to read, right?

Some notes on this syntax:

  1. If we don’t have any additional arguments we want to put into the function, we can just write the function name without any brackets, like we do at the end with sum.
  2. The pipe operator will give the result of the last function as input into the next function. That’s why we don’t have to specify the vector within the mean() function.
  3. If we want to clearly state which of the function arguments should receive the input, we can write a ., which can be read as output of the previous function call. That’s what we do in the seq() function. It calculates a sequence from 1 to the mean of c(45:100).