3 · Transform (exercises) — source
This page shows the source file of the exercise below. Use the button to download it as a plain file and run it in your own workspace.
---
title: "Transform Data"
---
```cmmlbyddy
#| include: false
library(tidyverse)
# Toy datasets to use
pollution <- tribble(
~city, ~size, ~amount,
"New York", "large", 23,
"New York", "small", 14,
"London", "large", 22,
"London", "small", 16,
"Beijing", "large", 121,
"Beijing", "small", 56
)
library(babynames)
```
# Import Data
## Your Turn 1
Make sure that the `.csv` file is in the same directory as the `.qmd` file you're currently working on.
Then import the `babynames.csv` data set. Give it the name `babynames`.
Then copy the import code into the code chunk below. Does it run?
```cfe
```
```cfe
babynames
```
# dplyr
## Your Turn 2
Alter the code to select just the `n` column:
```cfe
select(babynames, name, prop)
```
## Quiz
Which of these is NOT a way to select the `name` and `n` columns together?
```cfe
select(babynames, -c(year, sex, prop))
select(babynames, name:n)
select(babynames, starts_with("n"))
select(babynames, ends_with("n"))
```
## Your Turn 3
Use `filter`, `babynames`, and the logical operators to find:
* All of the names where prop is greater than or equal to 0.08
* All of the children named "Sea"
```cfe
```
## Your Turn 4
Use Boolean operators to return only the rows that contain:
* _Boys_ named Sue
* Names that were used by exactly 5 or 6 children in 1880
* Names that are one of Acura, Lexus, or Yugo
```cfe
```
## Help Me
What is the smallest value of n?
What is the largest?
```cfe
```
## Your Turn 5
Use `|>` to write a sequence of functions that:
1. Filters babynames to just the girls that were born in 2017, *then...*
2. Selects the `name` and `n` columns, *then...*
3. Arranges the results so that the most popular names are near the top.
```cfe
```
## Your Turn 6
1. Trim `babynames` to just the rows that contain your `name` and your `sex`
2. Trim the result to just the columns that will appear in your graph (not
strictly necessary, but useful practice)
3. Plot the results as a line graph with `year` on the x axis and `prop` on the
y axis
```cfe
```
## Your Turn 7
Cpmplete the code below to extract the rows where `name == "Khaleesi"`. Then
use `summarise()` and `sum()` and `min()` to find:
1. The total number of children named Khaleesi
2. The first year Khaleesi appeared in the data
Make sure to turn the `eval` option to `true` before running the code.
*(Hint: Be sure to remove each `_` before running the code)*
```cfe
#| eval: false
babynames ___
filter(_______________________) ___
___________(total = ________, first = _______)
```
## Your Turn 8
Use `group_by()`, `summarise()`, and `arrange()` to display the ten most popular
names. Compute popularity as the *total* number of children of a single gender
given a name.
Make sure to turn the `eval` option to `true` before running the code.
*(Hint: Be sure to remove each `_` before running the code)*
```cfe
#| eval: false
babynames |>
_______(name, sex) |>
_______(total = _____(n)) |>
_______(desc(_____))
```
## Your Turn 9
Use `group_by()` to calculate and then plot the total number of children born
each year over time.
```cfe
```
## Your Turn 10
Use `mutate()` and `min_rank()`to rank each row in babynames from _largest_ n to
lowest n.
Make sure to turn the `eval` option to `true` before running the code.
*(Hint: Be sure to remove each `_` before running the code)*
```cfe
#| eval: false
babynames |>
______(rank = _______(____(prop)))
```
## Your Turn 11
Group babynames by **year** and then re-rank the data. Filter the results to
just rows where **rank == 1**.
```cfe
```
***
# Take aways
* Extract variables with `select()`
* Extract cases with `filter()`
* Arrange cases, with `arrange()`
* Make tables of summaries with `summarise()`
* Make new variables, with `mutate()`
* Do groupwise operations with `group_by()`
* Connect operations with `|>`