Chapter 2 Tidyverse Basics

2.1 Learning objectives

By the end of this chapter, you should be able to:

  • load common R packages;
  • read data files into R;
  • inspect data frames;
  • use the pipe operator;
  • apply common dplyr verbs such as select, filter, mutate, group_by, and summarize.

2.2 Load libraries

library(tidyverse)

2.3 Read data

In most projects, the first step is reading raw data from the Raw Data/ folder.

example_data <- read_csv("./Raw Data/example.csv")

2.4 Inspect data

Useful functions include:

glimpse(example_data)
summary(example_data)
dim(example_data)
names(example_data)

2.5 Core tidyverse verbs

example_data %>%
  select(column_1, column_2) %>%
  filter(column_1 == "value") %>%
  mutate(new_column = column_2 * 2) %>%
  group_by(column_1) %>%
  summarize(total = sum(new_column, na.rm = TRUE))

2.6 Practice questions

  1. Read one .csv file from your Raw Data/ folder.
  2. Use glimpse() to inspect the dataset.
  3. Select three columns.
  4. Filter the data to one group or category.
  5. Create one new variable with mutate().