8.1 Mickey Mantle’s trajectory - Warm up

library(tidyverse)
library(Lahman)

mantle_id <- People |> 
  filter(nameFirst == "Mickey", nameLast == "Mantle") |>
  pull(playerID)

# deal with missing stats in early dat
batting <- Batting |>
  replace_na(list(SF = 0, HBP = 0))
  • Define a function to compute OPS = OBS + SLG and Age
get_stats <- function(player_id) {
  batting |> 
    filter(playerID == player_id) |>
    inner_join(People, by = "playerID") |>
    mutate(
      birthyear = if_else(
        birthMonth >= 7, birthYear + 1, birthYear
      ),
      Age = yearID - birthyear,
      SLG = (H - X2B - X3B - HR + 2 * X2B + 3 * X3B + 4 * HR) / AB,
      OBP = (H + BB + HBP) / (AB + BB + HBP + SF),
      OPS = SLG + OBP
    ) |>
    select(Age, SLG, OBP, OPS)
}
  • Finaly we can use this to look at OPS for Micky Mantle:
Mantle <- get_stats(mantle_id)
ggplot(Mantle, aes(Age, OPS)) + geom_point()