This package is designed to allow users to extract various world football results and player statistics from the following popular football (soccer) data sites:
You can install the worldfootballR
package from github with:
# install.packages("devtools")
::install_github("JaseZiv/worldfootballR") devtools
library(worldfootballR)
Package vignettes have been built to help you get started with the package.
This vignette will cover the functions to extract data from FBref.com.
To be able to join data player between FBref and Transfermarkt, player_dictionary_mapping()
has been created. There are over 6,100 players who have been listed for teams in the Big 5 Euro leagues on FBref since the start of the 2017-18 seasons, with all of these mapped together. This is expected to be updated and grow over time. The raw data is stored here
<- player_dictionary_mapping()
mapped_players ::glimpse(mapped_players)
dplyr#> Rows: 6,378
#> Columns: 4
#> $ PlayerFBref <chr> "Aaron Connolly", "Aaron Cresswell", "Aarón Escandell", "A…
#> $ UrlFBref <chr> "https://fbref.com/en/players/27c01749/Aaron-Connolly", "h…
#> $ UrlTmarkt <chr> "https://www.transfermarkt.com/aaron-connolly/profil/spiel…
#> $ TmPos <chr> "Centre-Forward", "Left-Back", "Goalkeeper", "Attacking Mi…
The following section will outline the various functions available to find different URLs to be able to pass through the FBref suite of functions outlined in this vignette.
To extract the URL of any country’s league(s) (provided fbref have data for the league), use the fb_league_urls()
function.
This function also accepts a tier
argument. for first-tier leagues, select ‘1st’, for second-tier select ‘2nd’ and so on.
A fill list of countries available can be found in the worldfootballR_data
repository and can be found here.
fb_league_urls(country = "ENG", gender = "M", season_end_year = 2021, tier = '2nd')
To get a list of URLs for each team in a particular season, the fb_teams_urls()
function can be used:
fb_teams_urls("https://fbref.com/en/comps/9/Premier-League-Stats")
To get a list of player URLs for a particular team, the fb_player_urls()
function can be used. The results of this output can be passed through to the player season stat functions fb_player_season_stats()
and fb_player_scouting_report()
.
fb_player_urls("https://fbref.com/en/squads/fd962109/Fulham-Stats")
To get the match URLs needed to pass in to some of the match-level functions below, get_match_urls()
can be used:
<- get_match_urls(country = "ENG", gender = "M", season_end_year = 2021, tier="1st") epl_2021_urls
This section will cover the functions to aid in the extraction of season team statistics.
The get_season_team_stats
function allows the user to return a data frame of different stat types for all teams in Domestic leagues here.
Note, some stats may not be available for all leagues. The big five European leagues should have all of these stats.
The following stat types can be selected:
# function to extract season teams stats
<- get_season_team_stats(country = "ENG", gender = "M", season_end_year = "2020", tier = "1st", stat_type = "shooting")
prem_2020_shooting # to get shooting stats for the English Championship:
<- get_season_team_stats(country = "ENG", gender = "M", season_end_year = "2020", tier = "2nd", stat_type = "shooting") championship_2020_shooting
The get_season_team_stats
function can be used to get data for multiple seasons/leagues/genders/etc.
Important to note, this function can only be used for one stat-type
at a time, however all other parameters can have multiple values:
<- get_season_team_stats(country = c("ENG", "ESP", "ITA", "GER", "FRA"),
big_5_2020_possessions gender = "M", season_end_year = 2020, tier = "1st", stat_type = "possession")
The fb_big5_advanced_season_stats()
function allows users to extract data for any of the below listed stat types for all teams of the big five European leagues (EPL, La Liga, Ligue 1, Serie A, Bundesliga).
The stat types available for this function are below:
The function also accepts a season or seasons and whether you want data for the player, or team.
Note that when selecting team_or_player="team"
, results will be returned for both the team’s for and against stats. To filter on this, use the Team_or_Opponent
column in the resulting data frame, selecting ‘team’ if you want the team’s for stats, or ‘opponent’ if you want the team’s against stats.
<- fb_big5_advanced_season_stats(season_end_year= c(2019:2021), stat_type= "shooting", team_or_player= "team") big5_team_shooting
The following sections outlines the functions available to extract data at the per-match level
To get the match results (and additional metadata) for all leagues and comps listed here, the following function can be used:
# function to extract Serie A match results data
<- get_match_results(country = "ITA", gender = "M", season_end_year = 2020, tier = "1st") serieA_2020
The function can also be used to return match URLs for a non-domestic league season. To use this functionality, simply leave country = ''
and pass the non-domestic league URL, which can be found at https://fbref.com/en/comps/
# for international friendlies:
get_match_results(country = "", gender = "M", season_end_year = 2018, tier = "", non_dom_league_url = "https://fbref.com/en/comps/218/history/Friendlies-M-Seasons")
The get_match_results()
function can be used to get data for multiple seasons/leagues/genders/etc also:
<- get_match_results(country = c("ENG", "ESP", "ITA", "GER", "FRA"),
big_5_2020_results gender = "M", season_end_year = 2020, tier = "1st")
This function will return similar results to that of get_match_results()
, however get_match_report()
will provide some additional information. It will also only provide it for a single match, not the whole season:
# function to extract match report data
<- get_match_report(match_url = "https://fbref.com/en/matches/47880eb7/Liverpool-Manchester-City-November-10-2019-Premier-League") liv_mci_2020
This function will return the main events that occur during a match, including goals, substitutions and red/yellow cards:
# function to extract match summary data
<- get_match_summary(match_url = "https://fbref.com/en/matches/47880eb7/Liverpool-Manchester-City-November-10-2019-Premier-League") liv_mci_2020_summary
This function will return a dataframe of all players listed for that match, including whether they started on the pitch, or on the bench.
From version 0.2.7, this function now also returns some summary performance data for each player that played, including their position, minutes played, goals, cards, etc.
# function to extract match lineups
<- get_match_lineups(match_url = "https://fbref.com/en/matches/47880eb7/Liverpool-Manchester-City-November-10-2019-Premier-League") liv_mci_2020_lineups
The below function allows users to extract shooting and shot creation event data for a match or selected matches. The data returned includes who took the shot, when, with which body part and from how far away. Additionally, the player creating the chance and also the creation before this are included in the data.
<- get_match_shooting(match_url = "https://fbref.com/en/matches/a3eb7a37/Sheffield-United-Wolverhampton-Wanderers-September-14-2020-Premier-League")
shot_one_match
<- c("https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1",
test_urls_multiple "https://fbref.com/en/matches/9cbccb37/Dijon-Angers-August-22-2020-Ligue-1",
"https://fbref.com/en/matches/f96cd5a0/Lorient-Strasbourg-August-23-2020-Ligue-1")
<- get_match_shooting(test_urls_multiple) shot_multiple_matches
The get_advanced_match_stats()
function allows the user to return a data frame of different stat types for matches played.
Note, some stats may not be available for all leagues. The big five European leagues should have all of these stats.
The following stat types can be selected:
The function can be used for either all players individually:
<- c("https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1",
test_urls_multiple "https://fbref.com/en/matches/9cbccb37/Dijon-Angers-August-22-2020-Ligue-1",
"https://fbref.com/en/matches/f96cd5a0/Lorient-Strasbourg-August-23-2020-Ligue-1")
<- get_advanced_match_stats(match_url = test_urls_multiple, stat_type = "possession", team_or_player = "player") advanced_match_stats
Or used for the team totals for each match:
<- c("https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1",
test_urls_multiple "https://fbref.com/en/matches/9cbccb37/Dijon-Angers-August-22-2020-Ligue-1",
"https://fbref.com/en/matches/f96cd5a0/Lorient-Strasbourg-August-23-2020-Ligue-1")
<- get_advanced_match_stats(match_url = test_urls_multiple, stat_type = "passing_types", team_or_player = "team") advanced_match_stats_team
This section will cover off the functions to get team-level data from FBref.
To get all the results a team(s) has competed in for a season, the following function can be used. The resulting data frame output will include all game results, including any cup games played, and will accept either one, or many team URLs.
# for single teams:
<- "https://fbref.com/en/squads/b8fd03ef/Manchester-City-Stats"
man_city_2021_url <- get_team_match_results(man_city_url)
man_city_2021_results # get all team URLs for a league
<- fb_teams_urls("https://fbref.com/en/comps/9/Premier-League-Stats")
epl_2021_team_urls <- get_team_match_results(team_url = team_urls) epl_2021_team_results
This section will cover the functions available to aid in the extraction of player season data.
The examples provided below in a lot of cases have the actual url (player or team) passed to them, however the suite of fbref helper functions outlined in this helpers vignette could also be used.
The fb_player_scouting_report()
function takes in two inputs;
player_url
- the URL of the player’s main pagepos_versus
which can return the player’s comparison against players in their “primary”, OR “secondary” positionand returns the full scouting report for the player selected.
As of version 0.3.6
, the function now returns the scouting report of ALL available periods, not just the “Last 365 Days”. As a result, there is now an additional column called scouting_period
. This column should be used to filter out the period/season you want the scouting report for:
# TO GET THE LAST 365 DAYS REPORT:
scout <- fb_player_scouting_report(player_url = "https://fbref.com/en/players/d70ce98e/Lionel-Messi",
pos_versus = "primary") %>%
dplyr::filter(scouting_period == "Last 365 Days")
::glimpse(fb_player_scouting_report(player_url = "https://fbref.com/en/players/d70ce98e/Lionel-Messi", pos_versus = "primary"))
dplyr::glimpse(fb_player_scouting_report(player_url = "https://fbref.com/en/players/d70ce98e/Lionel-Messi", pos_versus = "secondary")) dplyr
The fb_player_season_stats()
function allows for the extraction of historical season totals for selected player URLs and stat_type.
The stat_types available for use in this function are below:
<- fb_player_season_stats("https://fbref.com/en/players/e342ad68/Mohamed-Salah", stat_type = 'shooting')
mo_shooting
<- fb_player_season_stats(player_url = c("https://fbref.com/en/players/d70ce98e/Lionel-Messi",
multiple_playing_time "https://fbref.com/en/players/dea698d9/Cristiano-Ronaldo"),
stat_type = "playing_time")
The fb_big5_advanced_season_stats()
function allows users to extract data for any of the below listed stat types for all players of the big five European leagues (EPL, La Liga, Ligue 1, Serie A, Bundesliga).
The stat types available for this function are below:
The function also accepts a season or seasons and whether you want data for the player, or team.
<- fb_big5_advanced_season_stats(season_end_year= 2021, stat_type= "possession", team_or_player= "player") big5_player_possession
The fb_team_player_stats()
function allows users to extract data for any of the below listed stat types for all players of selected team(s) seasons,
The stat types available for this function are below:
# to get stats for just a single team
<- fb_team_player_stats(team_urls= "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'standard')
fleetwood_standard_stats
# Can even get stats for a series of teams:
<- fb_league_urls(country = "ENG", gender = "M",
league_url <- fb_teams_urls(league_url)
teams
<- fb_team_player_stats(team_urls= teams, stat_type= "playing_time") multiple_playing_time
The fb_player_match_logs()
function allows the user to return a data frame of the match logs of different stat types for a player’s matches played in a season.
The following stat types can be selected, depending on the player’s position (ie a striker probably won’t have “keepers” stats):
<- fb_player_match_logs("https://fbref.com/en/players/3bb7b8b4/Ederson", season_end_year = 2021, stat_type = 'summary') ederson_summary