Chapter 3 Handout 1, revisited

Handout 1 is (hopefully in retrospect) not too complicated. So, in this chapter we go over setup of the tidyverse and some basics on how to use it.

3.1 Introducing the Tidyverse

The tidyverse is a collection of data analysis packages. For convenience, the authors of the tidyverse allow you to access it with one simple install.

To install the tidyverse, run the following code in R once.

install.packages("tidyverse")

To get access to the tidyverse, run the following code before you start doing your work in R. It’s often helpful if you put it in/run it right before you load in your data in R.

library(tidyverse) # loads the tidyverse into your R environment
data <- mtcars # this is a dataset about cars
head(data)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

3.2 Pipes

One of the basic operators of the tidyverse is the pipe %>%.

Basically, the pipe tells R “take the result from the thing just before the pipe, and put it in as the first argument to the function right afterwards”. This sounds more complicated than it actually is, so let’s take an example from Handout 1:

How many rows and columns are in our dataset?

Without pipes, the answer would be:

# 32 rows, 11 columns
dim(data)
## [1] 32 11

Now, let’s see the same with a pipe:

# same exact thing
data %>%
  dim()
## [1] 32 11

Notice what the pipe is doing: it’s taking the result of what was before the pipe (the dataset data), and inputting it into the thing that follows (dim).

Clearly, in this situation, the pipe is a bit useless. Instead, the pipe is designed to handle situations where you will be making a lot of operations step-by-step, which we will go into in later handouts. For now, keep it in mind as we move into other handouts!