Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decode elias player ids #425

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: nflfastR
Title: Functions to Efficiently Access NFL Play by Play Data
Version: 4.5.1.9009
Version: 4.5.1.9010
Authors@R:
c(person(given = "Sebastian",
family = "Carl",
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- nflfastR now fully supports loading raw pbp data from local file system. The best way to use this feature is to set `options("nflfastR.raw_directory" = {"your/local/directory"})`. Alternatively, both `build_nflfastR_pbp()` and `fast_scraper()` support the argument `dir` which defaults to the above option. (#423)
- Added the new function `save_raw_pbp()` which efficiently downloads raw play-by-play data and saves it to the local file system. This serves as a helper to setup the system for faster play-by-play parsing via the above functionality. (#423)
- 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)


# nflfastR 4.5.1
Expand Down
21 changes: 15 additions & 6 deletions R/helper_decode_player_ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ decode_player_ids <- function(pbp, ..., fast = TRUE) {
),
decode_ids
)
} else if (any(season >= 9999)) {
# this isn't necessary now, but keeping this here in case the ID system changes

} else if (any(season >= 2023)) {
# need newer version of nflreadr to use load_players
rlang::check_installed("nflreadr (>= 1.3.0)", "to decode 2022ff player IDs.")
rlang::check_installed("nflreadr (>= 1.3.0)", "to decode 2023ff player IDs.")

players <- nflreadr::load_players()

extract_elias <- function(smart_id){
name_abbr <- gsisdecoder::decode_ids(smart_id) %>% substr(1,3)
id_no <- stringr::str_remove_all(smart_id, "-") %>%
stringr::str_sub(11, 16)
elias_id <- paste0(name_abbr, id_no)
elias_id
}

id_vector <- players$gsis_id
names(id_vector) <- players$smart_id
names(id_vector) <- players$esb_id

ret <- pbp %>%
dplyr::mutate_at(
Expand All @@ -88,7 +94,10 @@ decode_player_ids <- function(pbp, ..., fast = TRUE) {
chars <- nchar(id)
dplyr::case_when(
is.na(chars) ~ NA_character_,
chars == 36 ~ id_vec[id],
# this means it's gsis ID. 30 30 2d 30 30 translates to 00-00
stringr::str_sub(id, 5, 16) == "3030-2d30-30" ~ gsisdecoder::decode_ids(id),
# if it's not gsis, it is likely elias. We drop names to avoid confusion
nchar(id) == 36 ~ unname(id_vec[extract_elias(id)]),
TRUE ~ id
)
}
Expand Down
Loading