diff --git a/DESCRIPTION b/DESCRIPTION index f8ce77ab..683de994 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.9006 +Version: 4.5.1.9007 Authors@R: c(person(given = "Sebastian", family = "Carl", diff --git a/NEWS.md b/NEWS.md index bcb8aa97..fb031e7e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,7 @@ - The function `calculate_series_conversion_rates()` no longer returns `NA` values if a small subset of pbp data is missing series on offense or defense. (#417) - `fixed_drive` now correctly increments on plays where posteam lost a fumble but remains posteam because defteam also lost a fumble during the same play. (#419) - nflfastR now fixes missing drive number counts in raw pbp data in order to provide accurate drive information. (#420) +- nflfastR now returns `kick_distance` on punts and kickoffs resulting in touchbacks. (#421) # nflfastR 4.5.1 diff --git a/R/helper_add_nflscrapr_mutations.R b/R/helper_add_nflscrapr_mutations.R index 62a3ea39..2ebc1722 100644 --- a/R/helper_add_nflscrapr_mutations.R +++ b/R/helper_add_nflscrapr_mutations.R @@ -580,7 +580,21 @@ add_nflscrapr_mutations <- function(pbp) { !is.na(.data$qtr) ) %>% dplyr::ungroup() %>% - dplyr::mutate(game_id = as.character(.data$game_id)) %>% + dplyr::mutate( + game_id = as.character(.data$game_id), + # kick distance is NA on kickoffs and punts that result in touchbacks + # (unless the kick/punt) was caught between endzones + # we use yardline_100 to add it in those cases + is_relevant_touchback = as.numeric(is.na(.data$kick_distance) & .data$touchback == 1 & .data$play_type %in% c("punt", "kickoff")), + kick_distance = dplyr::case_when( + .data$is_relevant_touchback == 1 & .data$kickoff_attempt == 0 ~ yardline_100, + # gotta reverse yardline_100 on kickoffs + .data$is_relevant_touchback == 1 & .data$kickoff_attempt == 1 ~ 100 - yardline_100, + TRUE ~ .data$kick_distance + ), + # drop helper variable + is_relevant_touchback = NULL + ) %>% fix_scrambles() %>% make_model_mutations() diff --git a/tests/testthat/expected_pbp.rds b/tests/testthat/expected_pbp.rds index f354955b..78648134 100644 Binary files a/tests/testthat/expected_pbp.rds and b/tests/testthat/expected_pbp.rds differ