-
Notifications
You must be signed in to change notification settings - Fork 1
/
Aggregate-FBref-SinglePlayerMatches-witholdseasons.R
59 lines (39 loc) · 1.48 KB
/
Aggregate-FBref-SinglePlayerMatches-witholdseasons.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#remember to open the docker first!!!
source('FBref-scraper.R')
prevYrs=NULL #null for this season's data, 1 for last season's, 2 for two seasons ago, etc.
outfile='Pulisic1720Matches.csv'
years=c('2017-2018','2018-2019','2019-2020')
#formatting of these is important
baseurl='https://fbref.com/en/players/1bf33a9a/matchlogs/'
player='Christian-Pulisic'
################
### do work ####
################
## initial scraping ####
for (year in years){#league is the row index number within the leaguevals df
if (year==years[1]){#initialize df if its the first year in the list
matches<-getFBrefStats(
paste0(baseurl,year, "/summary/", player, "-Match-Logs"),
'#matchlogs_all')
matches$Year=year
}
else{#after that add to the df
tmp<-getFBrefStats(
paste0(baseurl,year, "/summary/", player, "-Match-Logs"),
'#matchlogs_all')
tmp$Year=year
matches<-rbind(matches, tmp)
}
}#end years loop
## reformat to make the table easier to use ####
formatted<-matches[-1,]
colnames(formatted)<-matches[1,]
library(tidyverse)
#rename a few of columns
torename<-which(colnames(formatted)%in%c("Succ",'Att','Cmp','Cmp%'))
colnames(formatted)[torename]<-paste0(colnames(matches)[torename], colnames(formatted)[torename])
#remove rows with variable names and empty rows and totals
formatted<-subset(formatted, Squad!="Squad")
formatted<-subset(formatted, Squad!="")
## save out ####
write.csv(formatted,outfile, row.names = F)