Extract seasonal shot data for one team from Understat in R

Extracting shot data made easy. Less than 10 lines of R code

Swaminathan Nanda-Kishore
1 min readJul 22, 2020

Do you want x and y coordinate data for one team’s shots in any particular season. Well, you can do that in just 5 lines of R code. Commence copy-paste

Step 1: Install this library from Ewen and load it up

#install the library from github
remotes::install_github('ewenme/understatr')library(understatr)
library(understatr)

Step 2 : Extract team data by mentioning your desired team within the quotations. Also mention the season you want. 2019 corresponds to 2019/20, 2018 to 2018/19 and so on. I chose Real Madrid and 2019.

rm = get_team_players_stats("Real Madrid",2019) #enter team name within quotations and season required

Step 3: Create a vector of all the players in the squad.

dato = c(rm$player_id)

Step 4: Create a preliminary table called shotx

shotx = get_player_shots(dato[1])

Step 5: For loop magic

for (i in 2:length(dato)) {
shoti = get_player_shots(dato[i])
shotx = rbind(shotx,shoti)
}

Step 6: Use the subset function to clear out data from older seasons

rmshotdata = subset(shotx,shotx$year == 2019)

Step 7: Extract your data as csv

setwd("~/Downloads") #set your desired export location herewrite.csv(rmshotdata,"RealMadrid2019shots.csv", row.names = FALSE)

And you’re done! Sorry, medium forced me to put a picture in here.

Photo by Clément H on Unsplash

--

--