Skip to content
Introduction to the Tidyverse
Introduction to the Tidyverse
Run the hidden code cell below to import the data used in this course.
# Load the Tidyverse
library(tidyverse)
# Read in the gapminder file
gapminder <- read.csv("datasets/gapminder.csv", sep = "\t")
Ch. 1: Data Wrangling
The filter
verb
filter
verb- used to look at a subset of observations based on a particular condition.
- used with pipe operator
%>%
==
is a logical equals.
# Filter for year 2007
gapminder %>%
filter(year == 2007)
# Filtering for one country
gapminder %>%
filter(country == "United States")
# Filtering for two variables
gapminder %>%
filter(year == 2007, country == "United States")
The arrange
verb
arrange
verb- sorts a table based on a variable
# Sorting with `arrange`
gapminder %>%
arrange(gdpPercap)
# Sorting in descending order
gapminder %>%
arrange(desc(gdpPercap))
# Filtering then arranging
gapminder %>%
filter(year == 2007) %>%
arrange(desc(gdpPercap))
The mutate
verb
mutate
verb- mutate changes or adds variables
# Using mutate to change a variable
gapminder %>%
mutate(pop = pop / 1000000)
# Using mutate to add a new variable
gapminder %>%
mutate(gdp = gdpPercap * pop)
# Combining verbs
gapminder %>%
mutate(gdp = gdpPercap * pop) %>%
filter(year == 2007) %>%
arrange(desc(gdp))
Chapter 2: Data Visualization
Visualizing with ggplot2
ggplot2