Lesson 13 of 17 · Week 06 · Tue Nov 3

dplyr I: filter, select, mutate

Questions

Objectives

Verbs for data frames

Base R subsetting works, but long chains of [ ] and $ get hard to read. The dplyr package gives you one function (a “verb”) per common task. This week covers the verbs that work on rows and columns; next week covers groups and joins.

VerbDoes
filter()keep rows that meet conditions
select()keep (or drop) columns
mutate()add or change columns
arrange()sort rows
slice_head(), pull()take the first n rows; extract one column as a vector

Every verb takes a data frame as its first argument, refers to columns by bare name (no gm$), and returns a new data frame. It never changes the original unless you assign the result back.

Adapted from Software Carpentry's “Data Frame Manipulation with dplyr” episode (CC BY 4.0, The Carpentries). Text, examples and exercises have been rewritten for this course.

The pipe

library(dplyr)
gm |>
  filter(continent == "Africa", year == 2007) |>
  select(country, lifeExp, gdpPercap)

The pipe |> takes the result on its left and passes it as the first argument of the function on its right. Read it as “and then”. The code above is the same as select(filter(gm, continent == "Africa", year == 2007), country, lifeExp, gdpPercap), just in reading order. You'll also see %>%, an older pipe that works the same way for everything in this course. In Positron, Ctrl/Cmd+Shift+M types a pipe.

filter() and select()

filter(gm, lifeExp > 75, year >= 2002)              # commas mean AND
filter(gm, continent %in% c("Asia", "Oceania"))
filter(gm, year == 1967 | year == 2007)             # | means OR
select(gm, country, year, lifeExp)
select(gm, -pop)                                    # everything except pop
select(gm, country, starts_with("gdp"))

mutate() and arrange()

gm |>
  mutate(pop_millions = pop / 1e6,
         gdp_billions = pop * gdpPercap / 1e9) |>
  arrange(desc(gdp_billions)) |>                    # largest first
  slice_head(n = 5)

Inside mutate(), a new column can use columns created earlier in the same call. arrange() sorts ascending; wrap a column in desc() for descending. pull(country) at the end of a pipe gives you a plain vector instead of a data frame.

Exercises

Each exercise gives you up to 10 checked attempts. Use Run as many times as you like to experiment first, then click Check answer when you're ready. Your exercises use numbers/subsets unique to you, so answers won't match a classmate's exactly.

Exercise 1 — One continent, three columns

0 / 10 attempts used

In a single pipe, make one_cont: the rows of gm for the continent shown when you click Run, with only the columns country, year and lifeExp (in that order). Then store its number of rows in n_rows. Why that number?

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Exercise 2 — Two new columns

0 / 10 attempts used

With one mutate(), add two columns to gm and store the result in gm2: pop_millions (population in millions) and gdp_billions (total GDP in billions of dollars). All 88 rows and the original columns should still be there.

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Exercise 3 — Top of the table

0 / 10 attempts used

Make top: the k countries with the highest life expectancy in year yr (click Run), highest first, with only the columns country and lifeExp. Use filter(), arrange(), slice_head() and select().

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Exercise 4 — Big economies

0 / 10 attempts used

Using one pipe, store in big the names (a character vector, via pull()) of the countries whose total GDP was above limit billion dollars in year yr (click Run). You'll need to compute total GDP first, then filter on it.

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Key Points

Proof of completion

Once you've attempted every exercise above, download a PDF summary of your work on this lesson — your name, the exercises you passed, attempt counts, timing, and your final code for each.