From ee4c346a9da9b85609c611341f2d976c14733434 Mon Sep 17 00:00:00 2001 From: Sebastian Carl Date: Mon, 11 Sep 2023 14:44:19 +0200 Subject: [PATCH 1/2] fix posteam for 2023ff --- R/helper_scrape_nfl.R | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/R/helper_scrape_nfl.R b/R/helper_scrape_nfl.R index 493db8ea..61236b70 100644 --- a/R/helper_scrape_nfl.R +++ b/R/helper_scrape_nfl.R @@ -287,7 +287,8 @@ get_pbp_nfl <- function(id, dplyr::mutate_if( .predicate = is.character, .funs = ~dplyr::na_if(.x, "") - ) + ) %>% + fix_posteams() # fix for games where home_team == away_team and fields are messed up if (bad_game == 1) { @@ -367,5 +368,33 @@ fix_bad_games <- function(pbp) { } +fix_posteams <- function(pbp){ + # 2023 pbp introduces two new problems + # 1. Definition of posteam on kick offs changed to receiving team. That's our + # definition and we swap teams later. + # 2. Posteam doesn't change on the PAT after defensive TD + # + # We adjust both things here, but only for 2023ff to avoid backwards compatibility problems + # We need the variable pre_play_by_play which usually looks like "KC 1-10 NYJ 40" + if (any(pbp$season >= 2023) && ("pre_play_by_play" %in% names(pbp))){ + # Let's be as explicit as possible about what we want to extract from the string + # It's really only the first valid team abbreviation followed by a blank space + valid_team_abbrs <- paste(nflfastR::teams_colors_logos$team_abbr, collapse = "|") + posteam_regex <- paste0("^", valid_team_abbrs, "(?=[:space:])") + + pbp <- pbp %>% + dplyr::mutate( + parsed_posteam = stringr::str_extract(.data$pre_play_by_play, posteam_regex), + posteam = dplyr::case_when( + is.na(.data$parsed_posteam) ~ .data$posteam, + .data$play_description == "GAME" ~ .data$posteam, + TRUE ~ .data$parsed_posteam + ), + # drop helper + parsed_posteam = NULL + ) + } + pbp +} From ed4872ec05c86d6da4f6852b3d6367d0327f809d Mon Sep 17 00:00:00 2001 From: Sebastian Carl Date: Mon, 11 Sep 2023 16:02:55 +0200 Subject: [PATCH 2/2] version and news bullet --- DESCRIPTION | 2 +- NEWS.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 86f720ef..6dbcaca7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: nflfastR Title: Functions to Efficiently Access NFL Play by Play Data -Version: 4.5.1.9011 +Version: 4.5.1.9012 Authors@R: c(person(given = "Sebastian", family = "Carl", diff --git a/NEWS.md b/NEWS.md index 54bd6505..6e6c8452 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,7 +13,7 @@ - Added the new function `missing_raw_pbp()` that computes a vector of game IDs missing in the local raw play-by-play directory. (#423) - Decode player IDs in 2023 pbp. (#425) - Drop the pseudo plays TV Timeout and Two-Minute Warning. (#426) - +- Fix posteam on kickoffs and PATs following a defensive TD in 2023+ pbp. (#427) # nflfastR 4.5.1