In this classwork, we’ll take a break from discussing probability and statistics to review the dplyr functions you’ve learned, along with introducing joins.
🕵️ A Crime Has Been Committed – Can You Help Solve It?
A crime has taken place, and the detective needs your help! You’ve been given special access to police data to crack the case. Let’s dive in and see if you can uncover the truth!
To help you navigate the data, here’s a database schema – a diagram that shows how the tables are connected. It’s like a map to guide you through the investigation:
👉 Tip: Click this link to open the schema in a new tab for easy reference as you work through the mystery.
Function toolbox:
pipe |>: takes the data from the left and passes it into the function on the right. Think of it as saying “and then”.
filter(): Use this to find rows that match specific conditions. For example:
income |> filter(annual_income > 100000) finds people earning over $100K.
people |> filter(name == "Christoper Peteuil") finds the person with that exact name.
str_detect(): Use this inside filter() for finding partial matches on character strings. For example: people |> filter(str_detect(name, "Amy")) finds everyone with the first name Amy.
arrange(): Sorts data from lowest to highest. For example:
income |> arrange(annual_income) sorts people by income (lowest first).
Add desc() to reverse the order: income |> arrange(desc(annual_income)) sorts from highest to lowest.
Chaining Steps: Combine multiple steps using pipes. For example: crime_scene_report |> filter(city == "Duluth") |> arrange(desc(date))This means: “Find crimes in Duluth, and then sort them by date (most recent first).”
# Review Examples# filter: find observations that fit any logical conditionincome |>filter(annual_income >100000)
# chain together multiple stepscrime_scene_report |>filter(city =="Duluth") |>arrange(desc(date))
# A tibble: 8 × 4
date type description city
<date> <chr> <chr> <chr>
1 2018-03-29 murder "Waiting in a hot tureen!\n" Dulu…
2 2018-02-06 assault "best to climb up one of the legs of the table, bu… Dulu…
3 2017-09-08 smuggling "‘Oh, don’t bother ME,’ said the Duchess; ‘I never… Dulu…
4 2017-07-23 blackmail "\n" Dulu…
5 2017-07-06 smuggling "a mouse--to a mouse--a mouse--O mouse!’) The Mous… Dulu…
6 2017-06-04 bribery "‘Thank you, it’s a very interesting dance to watc… Dulu…
7 2017-05-06 arson "\n" Dulu…
8 2017-02-07 arson "“There’s a porpoise close behind us, and he’s tre… Dulu…
👉 Tip: Click this link to open the examples in a new tab for easy reference.
Join Notes
Download to Solve on Your Own
Here’s the classwork (download R script). There are no test files for this assignment, so hitting Cmd/Ctrl+Shift+T will do nothing here.