Skip to content

Commit

Permalink
scrambles for older years
Browse files Browse the repository at this point in the history
  • Loading branch information
guga31bb committed Jun 13, 2024
1 parent 888b334 commit 516c88f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Binary file not shown.
36 changes: 28 additions & 8 deletions data-raw/build_scramble_fix.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
library(tidyverse)

pbp <- nflfastR::load_pbp(2005) %>%
dplyr::select(game_id, play_id, week, desc, away_team, home_team, posteam, qtr, down, ydstogo, time)
# yards gained needed to match some plays in older years but not available to match in 2005. set it equal to 0 for 2005

pbp <- nflfastR::load_pbp(1999 : 2005) %>%
select(season, game_id, play_id, week, away_team, home_team, posteam, qtr, down, ydstogo, time, yards_gained) |>
mutate(yards_gained = ifelse(season == 2005, 0, yards_gained))

# Thank you to Aaron Schatz and Football Outsiders
# For the charting data to fix scrambles in 2005
s <- readxl::read_xlsx("data-raw/scrambles_2005.xlsx") %>%
janitor::clean_names()
as_tibble() |>
janitor::clean_names() %>%
select(
season = year, week, qtr, away_team = away, home_team = home, posteam = offense, down, ydstogo = togo, date_time = time
) |>
mutate(yards_gained = 0)

dat <- s %>%
# Thank you to Aaron Schatz
# For the charting data to fix scrambles in 1999 - 2004
s2 <- readxl::read_xlsx("data-raw/Scrambles 1999-2004 UPDATE for NFLfastR.xlsx", sheet = 1) |>
as_tibble() |>
janitor::clean_names() |>
filter(type %in% c("scramble", "assume scramble")) %>%
select(
season = year, week, qtr, away_team = away, home_team = home, posteam = offense, down, ydstogo = togo, date_time = time, desc = description
) %>%
season = year, week, qtr, away_team = away, home_team = home, posteam = offense, down, ydstogo = togo, date_time = time, yards_gained = yards
)

dat <- bind_rows(
s2,
s
) %>%
mutate(
time = paste0(
formatC(lubridate::hour(date_time), width = 2, flag = "0"),
Expand All @@ -24,11 +42,13 @@ dat <- s %>%

d <- dat %>%
dplyr::left_join(
pbp %>% select(game_id, play_id, week, away_team, home_team, posteam, qtr, down, ydstogo, time),
by = c("week", "away_team", "home_team", "posteam", "qtr", "down", "ydstogo", "time")
pbp,
by = c("week", "away_team", "home_team", "posteam", "qtr", "down", "ydstogo", "time", "season", "yards_gained")
) %>%
mutate(scramble_id = paste0(game_id, "_", play_id)) %>%
filter(scramble_id != "2005_09_CIN_BAL_1725")

scramble_fix <- d$scramble_id
saveRDS(scramble_fix, file = "data-raw/scramble_fix.rds")


0 comments on commit 516c88f

Please sign in to comment.