/
global.R
308 lines (291 loc) · 16.4 KB
/
global.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
if (!require(httr)) {install.packages("httr")}
if (!require(XML)) {install.packages("XML")}
library(httr)
library(XML)
# Variables that can be put on the X and Y axes
axis_vars <- c(
"Points per game" = "PTS",
"Rebounds per game" = "TRB",
"Offensive rebounds per game" = "ORB",
"Defensive rebounds per game" = "DRB",
"Assists per game" = "AST",
"Steals per game" = "STL",
"Blocks per game" = "BLK",
"Effective field goal pct" = "eFGP",
"Field goals made per game" = "FG",
"Field goals attempts per game" = "FGA",
"Field goal pct" = "FGP",
"3 points shots made per game" = "TH",
"3 points shots attempts per game" = "THA",
"3 points shot pct" = "THP",
"2 points shots made per game" = "TW",
"2 points shots attempts per game" = "TWA",
"2 points shot pct" = "TWP",
"Free throws made per game" = "FT",
"Free throws attempts per game" = "FTA",
"Free throw pct" = "FTP",
"Turnovers per game" = "TOV",
"Fouls per game" = "FOU",
"True shooting pct" = "TSP",
"3 points attempt rate" = "TPAr",
"Free throw rate" = "FTr",
"Offensive rebound pct" = "ORBP",
"Defensive rebound pct" = "DRBP",
"Total rebound pct" = "TRBP",
"Assist pct" = "ASTP",
"Steal pct" = "STLP",
"Block pct" = "BLKP",
"Turnover pct" = "TOVP",
"Usage pct" = "USGP",
"Player efficiency rating" = "PER",
"Offensive win shares" = "OWS",
"Defensive win shares" = "DWS",
"Win shares" = "WS",
"Win shares per 48 mins" = "WSPFE",
"Offensive box plus/minus" = "OBPM",
"Defensive box plus/minus" = "DBPM",
"Box plus/minus" = "BPM",
"Value over replacement player" = "VORP")
# Retrieving NBA data from various sources and consolidate all info into one
# single CSV file
createCSV <- function(year)
{
# Retrieve basic stats from HTML source code
message("Stats parsing")
htmlDoc <- GET(paste("http://www.basketball-reference.com/leagues/NBA_", year, "_per_game.html", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
basicDoc = htmlParse(docContent)
# Retrieve advanced stats from HTML source code
message("Advanced stats parsing")
htmlDoc <- GET(paste("http://www.basketball-reference.com/leagues/NBA_", year, "_advanced.html", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
advancedDoc = htmlParse(docContent)
# Get Free Agency data from HTML source code
message("FA data parsing")
htmlDoc <- GET(paste("http://basketball.realgm.com/nba/future_free_agents/", year + 1, "/All/Per_Game/0/NBA/player", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
faTable <- xpathSApply(parsedDoc, "/descendant::tbody", xmlValue)
faVector <- unlist(strsplit(faTable, "\n"))
# Get Position data from HTML source code
message("Positions parsing")
htmlDoc <- GET(paste("http://sports.yahoo.com/nba/stats/byposition?pos=PG&conference=NBA&year=season_", year - 1, "&qualified=0", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
posTable <- xpathSApply(parsedDoc, "//div[@id='yog-bd']/div/div[2]/div/div/table[4]/tr/td/a", xmlValue)
pgVector <- unlist(strsplit(as.character(posTable), "\n"))
htmlDoc <- GET(paste("http://sports.yahoo.com/nba/stats/byposition?pos=SG&conference=NBA&year=season_", year - 1, "&qualified=0", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
posTable <- xpathSApply(parsedDoc, "//div[@id='yog-bd']/div/div[2]/div/div/table[4]/tr/td/a", xmlValue)
sgVector <- unlist(strsplit(as.character(posTable), "\n"))
htmlDoc <- GET(paste("http://sports.yahoo.com/nba/stats/byposition?pos=SF&conference=NBA&year=season_", year - 1, "&qualified=0", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
posTable <- xpathSApply(parsedDoc, "//div[@id='yog-bd']/div/div[2]/div/div/table[4]/tr/td/a", xmlValue)
sfVector <- unlist(strsplit(as.character(posTable), "\n"))
htmlDoc <- GET(paste("http://sports.yahoo.com/nba/stats/byposition?pos=PF&conference=NBA&year=season_", year - 1, "&qualified=0", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
posTable <- xpathSApply(parsedDoc, "//div[@id='yog-bd']/div/div[2]/div/div/table[4]/tr/td/a", xmlValue)
pfVector <- unlist(strsplit(as.character(posTable), "\n"))
htmlDoc <- GET(paste("http://sports.yahoo.com/nba/stats/byposition?pos=C&conference=NBA&year=season_", year - 1, "&qualified=0", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
posTable <- xpathSApply(parsedDoc, "//div[@id='yog-bd']/div/div[2]/div/div/table[4]/tr/td/a", xmlValue)
cVector <- unlist(strsplit(as.character(posTable), "\n"))
htmlDoc <- GET(paste("http://sports.yahoo.com/nba/stats/byposition?pos=G&conference=NBA&year=season_", year - 1, "&qualified=0", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
posTable <- xpathSApply(parsedDoc, "//div[@id='yog-bd']/div/div[2]/div/div/table[4]/tr/td/a", xmlValue)
gVector <- unlist(strsplit(as.character(posTable), "\n"))
htmlDoc <- GET(paste("http://sports.yahoo.com/nba/stats/byposition?pos=GF&conference=NBA&year=season_", year - 1, "&qualified=0", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
posTable <- xpathSApply(parsedDoc, "//div[@id='yog-bd']/div/div[2]/div/div/table[4]/tr/td/a", xmlValue)
gfVector <- unlist(strsplit(as.character(posTable), "\n"))
htmlDoc <- GET(paste("http://sports.yahoo.com/nba/stats/byposition?pos=F&conference=NBA&year=season_", year - 1, "&qualified=0", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
posTable <- xpathSApply(parsedDoc, "//div[@id='yog-bd']/div/div[2]/div/div/table[4]/tr/td/a", xmlValue)
fVector <- unlist(strsplit(as.character(posTable), "\n"))
htmlDoc <- GET(paste("http://sports.yahoo.com/nba/stats/byposition?pos=FC&conference=NBA&year=season_", year - 1, "&qualified=0", sep = ""))
stop_for_status(htmlDoc)
docContent <- content(htmlDoc, as = "text")
parsedDoc = htmlParse(docContent)
posTable <- xpathSApply(parsedDoc, "//div[@id='yog-bd']/div/div[2]/div/div/table[4]/tr/td/a", xmlValue)
fcVector <- unlist(strsplit(as.character(posTable), "\n"))
message("Parsing over")
# Create file with a header
file = paste("data/", year , "nbastats.csv", sep = "")
players <- data.frame(Rk = integer(), Player = factor(), Pos = factor(),
Age = integer(), Tm = factor(), G = integer(),
GS = integer(), MP = numeric(), FG = numeric(),
FGA = numeric(), FGP = numeric(), TH = numeric(),
THA = numeric(), THP = numeric(), TW = numeric(),
TWA = numeric(), TWP = numeric(), eFGP = numeric(),
FT = numeric(), FTA = numeric(), FTP = numeric(),
ORB = numeric(), DRB = numeric(), TRB = numeric(),
AST = numeric(), STL = numeric(), BLK = numeric(),
TOV = numeric(), FOU = numeric(), PTS = numeric(),
FA = integer(), PER = numeric(), TSP = numeric(),
TPAr = numeric(), FTr = numeric(), ORBP = numeric(),
DRBP = numeric(), TRBP = numeric(), ASTP = numeric(),
STLP = numeric(), BLKP = numeric(), TOVP = numeric(),
USGP = numeric(), OWS = numeric(), DWS = numeric(),
WS = numeric(), WSPFE = numeric(), OBPM = numeric(),
DBPM = numeric(), BPM = numeric(), VORP = numeric(),
PG = integer(), SG = integer(), SF = integer(),
PF = integer(), C = integer(),
stringsAsFactors=FALSE)
write.table(x = players,
file = file,
row.names = FALSE,
sep = ",")
# Populate CSV file
message("Populate CSV file")
playerNb <- as.integer(xpathSApply(basicDoc, "//tbody/tr[last()]/th", xmlValue))
message(paste("playerNb :", playerNb))
i <- 1
previousPlayer <- ""
while(i <= 1000)
{
curRk <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/th", sep = ""), xmlValue)
curPlayer <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[1]", sep = ""), xmlValue)
# Hack for lines in BBref code where they reprint stat names
if (length(curPlayer) == 0)
{
i <- i + 1
next()
}
else
{
# Hack for players traded during the season (only keep total stats)
if (curPlayer == previousPlayer)
{
i <- i + 1
next()
}
}
previousPlayer <- curPlayer
curPos <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[2]", sep = ""), xmlValue)
curAge <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[3]", sep = ""), xmlValue)
curTm <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[4]", sep = ""), xmlValue)
curG <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[5]", sep = ""), xmlValue)
curGS <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[6]", sep = ""), xmlValue)
curMP <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[7]", sep = ""), xmlValue)
curFG <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[8]", sep = ""), xmlValue)
curFGA <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[9]", sep = ""), xmlValue)
curFGP <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[10]", sep = ""), xmlValue)
cur3P <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[11]", sep = ""), xmlValue)
cur3PA <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[12]", sep = ""), xmlValue)
cur3PP <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[13]", sep = ""), xmlValue)
cur2P <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[14]", sep = ""), xmlValue)
cur2PA <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[15]", sep = ""), xmlValue)
cur2PP <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[16]", sep = ""), xmlValue)
curEFG <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[17]", sep = ""), xmlValue)
curFT <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[18]", sep = ""), xmlValue)
curFTA <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[19]", sep = ""), xmlValue)
curFTP <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[20]", sep = ""), xmlValue)
curORB <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[21]", sep = ""), xmlValue)
curDRB <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[22]", sep = ""), xmlValue)
curTRB <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[23]", sep = ""), xmlValue)
curAST <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[24]", sep = ""), xmlValue)
curSTL <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[25]", sep = ""), xmlValue)
curBLK <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[26]", sep = ""), xmlValue)
curTOV <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[27]", sep = ""), xmlValue)
curFOU <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[28]", sep = ""), xmlValue)
curPTS <- xpathSApply(basicDoc, paste("//tbody/tr[", i, "]/td[29]", sep = ""), xmlValue)
if (curFGP == "") {curFGP <- ".000"}
if (cur3PP == "") {cur3PP <- ".000"}
if (cur2PP == "") {cur2PP <- ".000"}
if (curEFG == "") {curEFG <- ".000"}
if (curFTP == "") {curFTP <- ".000"}
# Check for FA status
if (curPlayer %in% faVector) {curFA <- "1"}
else {curFA <- "0"}
curPER <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[7]", sep = ""), xmlValue)
curTSP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[8]", sep = ""), xmlValue)
curTPAr <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[9]", sep = ""), xmlValue)
curFTr <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[10]", sep = ""), xmlValue)
curORBP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[11]", sep = ""), xmlValue)
curDRBP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[12]", sep = ""), xmlValue)
curTRBP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[13]", sep = ""), xmlValue)
curASTP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[14]", sep = ""), xmlValue)
curSTLP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[15]", sep = ""), xmlValue)
curBLKP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[16]", sep = ""), xmlValue)
curTOVP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[17]", sep = ""), xmlValue)
curUSGP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[18]", sep = ""), xmlValue)
curOWS <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[20]", sep = ""), xmlValue)
curDWS <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[21]", sep = ""), xmlValue)
curWS <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[22]", sep = ""), xmlValue)
curWSPFE <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[23]", sep = ""), xmlValue)
curOBPM <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[25]", sep = ""), xmlValue)
curDBPM <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[26]", sep = ""), xmlValue)
curBPM <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[27]", sep = ""), xmlValue)
curVORP <- xpathSApply(advancedDoc, paste("//tbody/tr[", i, "]/td[28]", sep = ""), xmlValue)
if (curTSP == "") {curTSP <- ".000"}
if (curTPAr == "") {curTPAr <- ".000"}
if (curFTr == "") {curFTr <- ".000"}
if (curTOVP == "") {curTOVP <- ".000"}
# Check for position
curPG <- "0"
curSG <- "0"
curSF <- "0"
curPF <- "0"
curC <- "0"
if (curPlayer %in% pgVector) {curPG <- "1"}
if (curPlayer %in% sgVector) {curSG <- "1"}
if (curPlayer %in% sfVector) {curSF <- "1"}
if (curPlayer %in% pfVector) {curPF <- "1"}
if (curPlayer %in% cVector) {curC <- "1"}
if (curPlayer %in% gVector)
{
curPG <- "1"
curSG <- "1"
}
if (curPlayer %in% gfVector)
{
curSG <- "1"
curSF <- "1"
}
if (curPlayer %in% fVector)
{
curSF <- "1"
curPF <- "1"
}
if (curPlayer %in% fcVector)
{
curPF <- "1"
curC <- "1"
}
curMatrix<- matrix(
c(curRk, curPlayer, curPos, curAge, curTm, curG, curGS, curMP, curFG,
curFGA, curFGP, cur3P, cur3PA, cur3PP, cur2P, cur2PA, cur2PP, curEFG,
curFT, curFTA, curFTP, curORB, curDRB, curTRB, curAST, curSTL, curBLK,
curTOV, curFOU, curPTS, curFA, curPER, curTSP, curTPAr, curFTr, curORBP,
curDRBP, curTRBP, curASTP, curSTLP, curBLKP, curTOVP, curUSGP, curOWS,
curDWS, curWS, curWSPFE, curOBPM, curDBPM, curBPM, curVORP, curPG, curSG,
curSF, curPF, curC),
nrow = 1,
ncol = 56)
write.table(curMatrix,
file,
append = TRUE,
col.names = FALSE,
row.names = FALSE,
sep = ",")
i <- i + 1
}
}