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))
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)
}