sports <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-03-29/sports.csv')
## Rows: 132327 Columns: 28
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): institution_name, city_txt, state_cd, zip_text, classification_nam...
## dbl (20): year, unitid, classification_code, ef_male_count, ef_female_count,...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data <- sports %>%
filter(year == 2016, state_cd == "MA")
Unorder factor levels
# Transform data: calculate average total revenue by division
revmenwomen_by_division <- data %>%
group_by(classification_name) %>%
summarise(
avg_revmenwomen = mean(total_rev_menwomen, na.rm = TRUE)
)
revmenwomen_by_division
## # A tibble: 10 × 2
## classification_name avg_revmenwomen
## <chr> <dbl>
## 1 NAIA Division II 216348
## 2 NCAA Division I without football 2221522.
## 3 NCAA Division I-FBS 3050932.
## 4 NCAA Division I-FCS 1157692.
## 5 NCAA Division II with football 576039.
## 6 NCAA Division III with football 170922.
## 7 NCAA Division III without football 122192.
## 8 NJCAA Division III 31760.
## 9 Other 36836.
## 10 USCAA 15560.
# Plot
revmenwomen_by_division %>%
ggplot(aes(x = avg_revmenwomen, y = classification_name)) +
geom_point()
Ordered factor levels
revmenwomen_by_division %>%
ggplot(aes(x = avg_revmenwomen, y = fct_reorder(.f = classification_name, .x = avg_revmenwomen))) +
geom_point() +
# Labeling
labs(y = NULL, x = "Average Total Revenue by School Division")
Show examples of three functions:
data %>%
distinct(institution_name)
## # A tibble: 65 × 1
## institution_name
## <chr>
## 1 American International College
## 2 Amherst College
## 3 Anna Maria College
## 4 Assumption College
## 5 Babson College
## 6 Becker College
## 7 Bentley University
## 8 Boston College
## 9 Boston University
## 10 Brandeis University
## # … with 55 more rows
# Recode
data %>%
# Rename levels
mutate(BC = fct_recode(institution_name, "BC" = "Boston College")) %>%
select(institution_name, BC) %>%
filter(institution_name == "Boston College")
## # A tibble: 17 × 2
## institution_name BC
## <chr> <fct>
## 1 Boston College BC
## 2 Boston College BC
## 3 Boston College BC
## 4 Boston College BC
## 5 Boston College BC
## 6 Boston College BC
## 7 Boston College BC
## 8 Boston College BC
## 9 Boston College BC
## 10 Boston College BC
## 11 Boston College BC
## 12 Boston College BC
## 13 Boston College BC
## 14 Boston College BC
## 15 Boston College BC
## 16 Boston College BC
## 17 Boston College BC
# Collapse multiple levels into one
data %>%
mutate(sector = fct_collapse(sector_name, "Public" = c("Public, 4-year or above", "Public, 2-year"))) %>%
select(sector_name, sector) %>%
filter(sector_name != "Private nonprofit, 4-year or above")
## # A tibble: 182 × 2
## sector_name sector
## <chr> <fct>
## 1 Public, 4-year or above Public
## 2 Public, 4-year or above Public
## 3 Public, 4-year or above Public
## 4 Public, 4-year or above Public
## 5 Public, 4-year or above Public
## 6 Public, 4-year or above Public
## 7 Public, 4-year or above Public
## 8 Public, 4-year or above Public
## 9 Public, 4-year or above Public
## 10 Public, 4-year or above Public
## # … with 172 more rows
# Lump small levels into other levels
data %>%
count(sports)
## # A tibble: 30 × 2
## sports n
## <chr> <int>
## 1 All Track Combined 22
## 2 Baseball 54
## 3 Basketball 63
## 4 Diving 1
## 5 Equestrian 5
## 6 Fencing 5
## 7 Field Hockey 39
## 8 Football 30
## 9 Golf 32
## 10 Gymnastics 1
## # … with 20 more rows
data %>%
mutate(sports_lump = fct_lump(sports)) %>%
distinct(sports_lump)
## # A tibble: 30 × 1
## sports_lump
## <fct>
## 1 Baseball
## 2 Basketball
## 3 All Track Combined
## 4 Field Hockey
## 5 Football
## 6 Golf
## 7 Ice Hockey
## 8 Lacrosse
## 9 Soccer
## 10 Softball
## # … with 20 more rows
No need to do anything here.