If you are interested in football data analytics, the worldfootballR package in R is one of the most powerful tools you can use. It allows you to collect and analyze soccer data from FBref, Transfermarkt, and Understat with just a few lines of code. In this step-by-step guide, we will show you how to install and use worldfootballR effectively.
Step 1: Install R and RStudio
Before using worldfootballR, make sure you have installed the latest versions of R and RStudio. You can download them here:
Step 2: Install worldfootballR
You can install worldfootballR from CRAN with a single command:
install.packages("worldfootballR")
If you want the latest development version from GitHub, use:
# Make sure you have devtools
install.packages("devtools")
# Install the development version
devtools::install_github("JaseZiv/worldfootballR")
Step 3: Load the Package
Once installed, load the package into your R session:
library(worldfootballR)
Step 4: Fetch Data from FBref
Letβs get started with an example. Suppose you want to download shooting statistics for the English Premier League:
# Get Premier League shooting stats for 2024/25
epl_shooting <- fb_season_team_stats(
country = "ENG",
gender = "M",
season_end_year = 2025,
tier = "1st",
stat_type = "shooting"
)
head(epl_shooting)
Step 5: Collect Transfermarkt Data
You can also fetch transfer histories and market values:
# Example: Bayern Munich transfer history
team_players <- tm_team_player_urls("https://www.transfermarkt.com/fc-bayern-munchen/startseite/verein/27")
transfers <- tm_player_transfer_history(player_urls = team_players)
head(transfers)
Step 6: Work with Understat xG Data
Understat provides expected goals (xG) and shot maps:
# Example: Premier League shots from Understat
epl_shots <- load_understat_league_shots(league = "EPL", season_start_year = 2024)
head(epl_shots)
Best Practices
- Respect data sources and avoid sending too many requests too quickly.
- Cache your results locally to save time and avoid repeated scraping.
- Always check Terms of Service before heavy usage.
Conclusion
The worldfootballR package is an essential tool for anyone working with football data in R. From player stats and team analysis to transfer history and expected goals, you can build complete analytics projects with ease.
π Take Your Football Analytics to the Next Level
If you want to go beyond basic tutorials and learn how to build end-to-end football analytics projects with worldfootballR, check out our book:
Mastering Football Data with worldfootballR
This 53-page practical guide will teach you how to collect, clean, and analyze football data using R, with step-by-step examples from FBref, Transfermarkt, and Understat.