10.2 Finding slumps for all players

Create a function to find the “Longest 0fer”

longest_ofer <- function(batter) {
  retro2016 |> 
    dplyr::filter(bat_id == batter, ab_fl == TRUE) |> 
    dplyr::mutate(
      H = ifelse(h_fl > 0, 1, 0),
      date = substr(game_id, 4, 12)
    ) |> 
    dplyr::arrange(date) |> 
    dplyr::pull(H) |> 
    streaks() |> 
    dplyr::filter(values == 0) |> 
    dplyr::summarize(max_streak = max(lengths))
}

Verify that it works for Ichiro…

longest_ofer("suzui001")
## # A tibble: 1 × 1
##   max_streak
##        <int>
## 1         18

Use the function on players with 400 ABs or more

players_400 <- retro2016 |> 
  dplyr::group_by(bat_id) |> 
  dplyr::summarize(AB = sum(ab_fl)) |> 
  dplyr::filter(AB >= 400) |> 
  dplyr::pull(bat_id)
reg_streaks <- players_400 |>
  purrr::set_names() |>
  purrr::map(longest_ofer) |>
  purrr::list_rbind() |>
  dplyr::mutate(bat_id = players_400)

Inner join with Lahman dataset to get names and then look at the longest streaks

library(Lahman)
reg_streaks |>
  dplyr::inner_join(People, by = c("bat_id" = "retroID")) |> 
  dplyr::mutate(Name = paste(nameFirst, nameLast)) |> 
  dplyr::arrange(desc(max_streak)) |>
  dplyr::select(Name, max_streak) |>
  dplyr::slice_head(n = 6)
## # A tibble: 6 × 2
##   Name             max_streak
##   <chr>                 <int>
## 1 Carlos Beltran           32
## 2 Denard Span              30
## 3 Brandon Moss             29
## 4 Eugenio Suarez           28
## 5 Francisco Lindor         27
## 6 Albert Pujols            26