-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #386 from jruizcabrejos/main
Understat match stats and players
- Loading branch information
Showing
9 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Type: Package | ||
Package: worldfootballR | ||
Title: Extract and Clean World Football (Soccer) Data | ||
Version: 0.6.5.0005 | ||
Version: 0.6.5.0006 | ||
Authors@R: c( | ||
person("Jason", "Zivkovic", , "[email protected]", role = c("aut", "cre", "cph")), | ||
person("Tony", "ElHabr", , "[email protected]", role = "ctb"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
#' Get Understat match player data | ||
#' | ||
#' Returns player values for a selected match from Understat.com. | ||
#' | ||
#' @param match_url A `character` string with the URL of the match played. | ||
#' | ||
#' @return returns a `data.frame` with data for all players for the match. | ||
#' | ||
#' @importFrom magrittr %>% | ||
#' @importFrom rvest html_elements | ||
#' @importFrom stringi stri_unescape_unicode | ||
#' @importFrom stringr str_subset | ||
#' @importFrom jsonlite fromJSON | ||
#' @importFrom dplyr bind_rows | ||
#' | ||
#' @export | ||
|
||
understat_match_players <- function(match_url) { | ||
|
||
match_id <- gsub("[^0-9]", "", match_url) | ||
|
||
match_player_data <- .get_understat_json(page_url = match_url) %>% | ||
rvest::html_elements("script") %>% | ||
as.character() | ||
|
||
match_player_data <- match_player_data[grep("rostersData\t=", match_player_data)] %>% | ||
stringi::stri_unescape_unicode() %>% | ||
substr(41,nchar(.)) %>% | ||
substr(0,nchar(.)-13) %>% | ||
paste0('[', . , ']') %>% | ||
|
||
unlist() %>% | ||
stringr::str_subset("\\[\\]", negate = TRUE) | ||
|
||
match_player_data <- lapply(match_player_data, jsonlite::fromJSON) %>% | ||
do.call("rbind", .) | ||
|
||
match_player_data_home <- do.call(rbind.data.frame, match_player_data$h) | ||
match_player_data_away <- do.call(rbind.data.frame, match_player_data$a) | ||
|
||
|
||
match_player_data_rebind <- dplyr::bind_rows(match_player_data_home, match_player_data_away) | ||
|
||
match_players <- data.frame( | ||
match_id = as.integer(match_id), | ||
id = as.integer(match_player_data_rebind[["id"]]), | ||
team_id = as.integer(match_player_data_rebind[["team_id"]]), | ||
home_away = as.character(match_player_data_rebind[["h_a"]]), | ||
player_id = as.integer(match_player_data_rebind[["player_id"]]), | ||
swap_id = as.integer(match_player_data_rebind[["id"]]), | ||
player = as.character(match_player_data_rebind[["player"]]), | ||
position = as.character(match_player_data_rebind[["position"]]), | ||
positionOrder = as.integer(match_player_data_rebind[["positionOrder"]]), | ||
time_played = as.integer(match_player_data_rebind[["time"]]), | ||
goals = as.integer(match_player_data_rebind[["goals"]]), | ||
own_goals = as.integer(match_player_data_rebind[["own_goals"]]), | ||
shots = as.integer(match_player_data_rebind[["shots"]]), | ||
xG = as.numeric(match_player_data_rebind[["xG"]]), | ||
yellow_card = as.integer(match_player_data_rebind[["yellow_card"]]), | ||
red_card = as.integer(match_player_data_rebind[["red_card"]]), | ||
roster_in = as.integer(match_player_data_rebind[["roster_in"]]), | ||
roster_out = as.integer(match_player_data_rebind[["roster_out"]]), | ||
key_passes = as.integer(match_player_data_rebind[["key_passes"]]), | ||
assists = as.integer(match_player_data_rebind[["assists"]]), | ||
xA = as.numeric(match_player_data_rebind[["xA"]]), | ||
xGChain = as.numeric(match_player_data_rebind[["xGChain"]]), | ||
xGBuildup = as.numeric(match_player_data_rebind[["xGBuildup"]]) | ||
) | ||
|
||
return(match_players) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
#' Get Understat match stats table data | ||
#' | ||
#' Returns the Stats values for a selected match from Understat.com. | ||
#' | ||
#' @param match_url A `character` string with the URL of the match played. | ||
#' | ||
#' @return returns a `data.frame` with data from the stats table for the match. | ||
#' | ||
#' @details For `draw_chances`, `home_chances` and `away_chances`, values below 10% in the browser will be retrieved as NA (e.g. A "5%" chance will be NA in the `data.frame`). | ||
#' | ||
#' @importFrom magrittr %>% | ||
#' @importFrom rvest html_elements html_text | ||
#' | ||
#' @export | ||
|
||
understat_match_stats <- function(match_url) { | ||
|
||
match_stats <- .get_understat_json(page_url = match_url) %>% | ||
rvest::html_elements("div.scheme-block.is-hide[data-scheme='stats']") %>% | ||
rvest::html_elements(".progress-value") %>% | ||
rvest::html_text() | ||
|
||
away <- match_stats[seq(1, length(match_stats), by=2)] | ||
home <- match_stats[seq(2, length(match_stats), by=2)] | ||
|
||
match_stats <- data.frame( | ||
|
||
match_id = as.integer(gsub("[^0-9]", "", match_url)), | ||
|
||
home_team = as.character(away[1]), | ||
home_chances = as.integer(gsub("[^0-9]", "", away[2]))/100, | ||
home_goals = as.integer(home[3]), | ||
home_xG = as.numeric(home[4]), | ||
home_shots = as.integer(home[5]), | ||
home_shot_on_target = as.integer(home[6]), | ||
home_deep = as.integer(home[7]), | ||
home_PPDA = as.numeric(home[8]), | ||
home_xPTS = as.numeric(home[9]), | ||
|
||
draw_chances = as.integer(gsub("[^0-9]", "", home[2]))/100, | ||
|
||
away_team = home[1], | ||
away_chances = as.integer(gsub("[^0-9]", "", away[3]))/100 , | ||
away_goals = as.integer(away[4]), | ||
away_xG = as.numeric(away[5]), | ||
away_shots = as.integer(away[6]), | ||
away_shot_on_target = as.integer(away[7]), | ||
away_deep = as.integer(away[8]), | ||
away_PPDA = as.numeric(away[9]), | ||
away_xPTS = as.numeric(away[10]) | ||
|
||
) | ||
|
||
return(match_stats) | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters