Import your data

standings <- read_excel("../00_data/nfl_standings.xlsx")

Chapter 15

Create a factor

Modify factor order

Make two bar charts here - one before ordering another after

# Mean number of wins per year
standings_avg <- standings %>%
    group_by(team_name) %>%
    summarise(avg_wins = mean(wins, na.rm = TRUE)
    )
standings_avg
## # A tibble: 32 × 2
##    team_name  avg_wins
##    <chr>         <dbl>
##  1 49ers          7.4 
##  2 Bears          7.85
##  3 Bengals        7.25
##  4 Bills          6.85
##  5 Broncos        9.1 
##  6 Browns         4.95
##  7 Buccaneers     6.9 
##  8 Cardinals      6.85
##  9 Chargers       8.1 
## 10 Chiefs         8.3 
## # ℹ 22 more rows
# Before fct_reorder
standings_avg %>%
    ggplot(aes(x = avg_wins,
               y = team_name, .x = avg_wins)) +
    geom_point() +
    
    labs(y = NULL, x = "Wins by Team")

# With fct_reorder, before moving Patriots individually
standings_avg %>%
    ggplot(aes(x = avg_wins,
               y = fct_reorder(.f = team_name, .x = avg_wins))) +
    geom_point() +
    
    labs(y = NULL, x = "Wins by Team")

# After moving Patriots
standings_avg %>%
    ggplot(aes(x = avg_wins,
               y = fct_reorder(.f = team_name, .x = avg_wins) %>%
                   fct_relevel("Patriots"))) +
    geom_point() +
    
    labs(y = NULL, x = "Wins by Team")

Modify factor levels

Show examples of three functions:

  • fct_recode
  • fct_collapse
  • fct_lump
standings %>% distinct(sb_winner)
## # A tibble: 2 × 1
##   sb_winner    
##   <chr>        
## 1 No Superbowl 
## 2 Won Superbowl
# Recode
standings %>% 
    
    # Rename levels
    mutate(sb_rev = fct_recode(sb_winner, "Better luck next year!" = "No Superbowl")) %>% 
    select(sb_winner, sb_rev) %>% 
    filter(sb_winner == "No Superbowl")
## # A tibble: 618 × 2
##    sb_winner    sb_rev                
##    <chr>        <fct>                 
##  1 No Superbowl Better luck next year!
##  2 No Superbowl Better luck next year!
##  3 No Superbowl Better luck next year!
##  4 No Superbowl Better luck next year!
##  5 No Superbowl Better luck next year!
##  6 No Superbowl Better luck next year!
##  7 No Superbowl Better luck next year!
##  8 No Superbowl Better luck next year!
##  9 No Superbowl Better luck next year!
## 10 No Superbowl Better luck next year!
## # ℹ 608 more rows
# Collapse multiple levels into one
standings %>%
    
    mutate(ny_teams = fct_collapse(team_name, "New-Yorkers" = c("Giants", "Jets", "Bills"))) %>%
    select(team, team_name, ny_teams) %>%
    filter(team == "New York")
## # A tibble: 40 × 3
##    team     team_name ny_teams   
##    <chr>    <chr>     <fct>      
##  1 New York Jets      New-Yorkers
##  2 New York Giants    New-Yorkers
##  3 New York Jets      New-Yorkers
##  4 New York Giants    New-Yorkers
##  5 New York Jets      New-Yorkers
##  6 New York Giants    New-Yorkers
##  7 New York Jets      New-Yorkers
##  8 New York Giants    New-Yorkers
##  9 New York Jets      New-Yorkers
## 10 New York Giants    New-Yorkers
## # ℹ 30 more rows
#Lump small levels into other levels
standings %>% count(team_name)
## # A tibble: 32 × 2
##    team_name      n
##    <chr>      <int>
##  1 49ers         20
##  2 Bears         20
##  3 Bengals       20
##  4 Bills         20
##  5 Broncos       20
##  6 Browns        20
##  7 Buccaneers    20
##  8 Cardinals     20
##  9 Chargers      20
## 10 Chiefs        20
## # ℹ 22 more rows
standings %>% mutate(playoff_lump = fct_lump(playoffs)) %>% distinct(playoff_lump)
## # A tibble: 2 × 1
##   playoff_lump
##   <fct>       
## 1 Other       
## 2 No Playoffs
#Now small amount of teams that made playoffs are classified as "Other"

Chapter 16

No need to do anything here.