Silly? Yes. But let’s practice tidyverse
Explore data about cats using dyplr tidy functions: summarize, group_by, mutate.
Calculate course grades using case_when
Load packages
Load data (link to download cats data)
What is in the data frame?
Dplyr summarize()
# A tibble: 1 × 1
avg_cats
<dbl>
1 NA
Dplyr group_by()
# A tibble: 60 × 3
# Groups: handedness [3]
name number_of_cats handedness
<chr> <chr> <chr>
1 Bernice Warren 0 left
2 Woodrow Stone 0 left
3 Willie Bass 1 left
4 Tyrone Estrada 3 left
5 Alex Daniels 3 left
6 Jane Bates 2 left
7 Latoya Simpson 1 left
8 Darin Woods 1 left
9 Agnes Cobb 0 left
10 Tabitha Grant 0 left
# ℹ 50 more rows
group_by ?# A tibble: 60 × 3
# Groups: handedness [3]
name number_of_cats handedness
<chr> <chr> <chr>
1 Bernice Warren 0 left
2 Woodrow Stone 0 left
3 Willie Bass 1 left
4 Tyrone Estrada 3 left
5 Alex Daniels 3 left
6 Jane Bates 2 left
7 Latoya Simpson 1 left
8 Darin Woods 1 left
9 Agnes Cobb 0 left
10 Tabitha Grant 0 left
# ℹ 50 more rows
What’s a tibble?
tbltbl, pronounced “tibble”, is a special kind of data frame.group_by and summarize always return this type of data frame.Do people with different “handedness” own more/less cats?
case_when(condition ~ output_value)
condition is the condition that evaluates as TRUE (the “if”)
output_value is the value to output if the condition is TRUE (the “then”)
df <- data.frame(
student = c("Natascha", "Alex", "Arun", "Arturo", "Ashley", "Oscar", "James", "Elliot"),
score = c(92, 78, 85, 86, 93, 67, 56, 73))
df %>%
mutate(grade = case_when(
score >= 90 ~ 'A',
score >= 80 ~ 'B',
score >= 70 ~ 'C',
score >= 60 ~ 'D',
TRUE ~ 'F')) student score grade
1 Natascha 92 A
2 Alex 78 C
3 Arun 85 B
4 Arturo 86 B
5 Ashley 93 A
6 Oscar 67 D
7 James 56 F
8 Elliot 73 C
case_when to recodecase_when() function