-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitial load awards.R
214 lines (187 loc) · 7.44 KB
/
initial load awards.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
library(rvest)
library(tidyverse)
library(janitor)
library(polite)
source("award shares.R")
current_year=2024
psi <- read_csv("Data/Player Season Info.csv")
# add nba mvps
mvp <- tibble()
sapply(current_year:1956, function(x) {
new_seas <- get_award_pcts_mvp_roy(season=x,award = "mvp",lg="nba")
mvp <<- bind_rows(mvp, new_seas)
print(x)
})
# add aba mvps
sapply(1976:1968, function(x) {
new_seas <- get_award_pcts_mvp_roy(season=x, award = "mvp", lg="aba")
mvp <<- rbind(mvp, new_seas)
print(x)
})
# add nba roys
roy=tibble()
sapply(c(current_year:1964, 1959), function(x) {
new_seas <- get_award_pcts_mvp_roy(season=x, award="roy",lg="nba")
roy <<- rbind(roy, new_seas)
print(x)
})
# add aba roys
sapply(1975:1972, function(x) {
new_seas <- get_award_pcts_mvp_roy(season=x, award="roy", lg="aba")
roy <<- rbind(roy, new_seas)
print(x)
})
# add nba roy winners with no voting results
session=nod(bbref_bow,path="awards/roy.html")
nba_roys_without_voting <- scrape(session) %>%
html_nodes(css = "#div_roy_NBA") %>%
html_table() %>% .[[1]] %>%
# first row is actual column names
row_to_names(1) %>% clean_names() %>%
filter(voting != "(V)") %>%
mutate(season = as.numeric(substr(season, 0, 4)) + 1) %>%
# remove asterisks (unofficial recognize)
mutate(player = ifelse(str_detect(player, "\\*"), substr(player, 1, nchar(player) - 2), player)) %>%
# remove trailing tie in 1952
mutate(player = ifelse(str_detect(player, "(Tie)"), substr(player, 1, nchar(player) - 6), player)) %>%
select(-c(voting, g:ws_48)) %>%
mutate(award = ifelse(lg == "NBA", "nba roy", "baa roy")) %>%
relocate(award, .before = player) %>%
select(-lg) %>%
add_column("winner" = TRUE) %>% mutate(age=as.numeric(age))
# add aba roy winners with no voting results
aba_roys_without_voting <- scrape(session) %>%
html_nodes(css = "#div_roy_ABA") %>%
html_table() %>% .[[1]] %>%
# first row is actual column names
row_to_names(1) %>% clean_names() %>%
filter(voting != "(V)") %>%
mutate(season = as.numeric(substr(season, 0, 4)) + 1) %>%
# 1971 was a tie, so get rid of trailing (Tie) remark for Charlie Scott and Dan Issel
mutate(player = ifelse(str_detect(player, "(Tie)"), substr(player, 1, nchar(player) - 6), player)) %>%
select(-c(voting, g:ws_48)) %>%
add_column(award = "aba roy", .before = "player") %>%
select(-lg) %>%
add_column("winner" = TRUE) %>% mutate(age=as.numeric(age))
final_roys <- bind_rows(roy, nba_roys_without_voting,aba_roys_without_voting)
mip <- tibble()
sapply(current_year:1986, function(x) {
new_seas <- get_award_pcts_other(season=x, award="mip")
mip <<- rbind(mip, new_seas)
print(x)
})
dpoy <- tibble()
sapply(current_year:1983, function(x) {
new_seas <- get_award_pcts_other(x, "dpoy")
dpoy <<- rbind(dpoy, new_seas)
print(x)
})
smoy <- tibble()
# no voting found for 1983
sapply(current_year:1984, function(x) {
new_seas <- get_award_pcts_other(x, "smoy")
smoy <<- rbind(smoy, new_seas)
print(x)
})
smoy <- smoy %>% add_row(season = 1983, award = "smoy", player = "Bobby Jones", age = 31, tm = "PHI", winner = TRUE)
awards <- bind_rows(dpoy, smoy, mip, mvp, roy) %>%
arrange(desc(season),award,desc(share)) %>%
mutate(player=case_when(
(player == "Jaren Jackson" & season >=2019)~"Jaren Jackson Jr.",
(player == "Marvin Bagley" & season >= 2019)~"Marvin Bagley III",
(player == "Dennis Smith" & season >= 2018)~"Dennis Smith Jr.",
(player == "Taurean Waller-Prince" & season >= 2018)~"Taurean Prince",
(player == "Tim Hardaway" & season >= 2014)~"Tim Hardaway Jr.",
(player == "Nenê Hilário" & season >= 2003)~"Nenê",
(player == "Michael Porter" & season >= 2020)~"Michael Porter Jr.",
TRUE~player)
) %>%
left_join(., psi) %>%
select(-c(birth_year:experience))
write_csv(awards, "Data/Player Award Shares.csv")
all_lg <- tibble()
sapply(current_year:1966, function(x) {
new_seas <- all_lg_voting(season=x,award="all_nba")
all_lg <<- rbind(all_lg, new_seas)
print(x)
})
all_def_voting <- tibble()
sapply(current_year:1969, function(x) {
new_seas <- all_lg_voting(season=x,award="all_defense")
all_def_voting <<- rbind(all_def_voting, new_seas)
print(x)
})
all_rook_voting <- tibble()
sapply(current_year:1963, function(x) {
new_seas <- all_lg_voting(season=x,award="all_rookie")
all_rook_voting <<- rbind(all_rook_voting, new_seas)
print(x)
})
full_all_lg<-bind_rows(all_lg,all_def_voting,all_rook_voting) %>%
left_join(.,psi) %>%
mutate(tm = ifelse(tm == "TOT", "1TOT", tm)) %>%
group_by(player_id, season,type) %>%
arrange(tm) %>%
slice(1) %>%
mutate(tm = ifelse(tm == "1TOT", "TOT", tm)) %>% ungroup() %>%
arrange(desc(season), type, number_tm,desc(share)) %>% select(-c(birth_year:experience))
write_csv(full_all_lg,"Data/End of Season Teams (Voting).csv")
all_lg_without_voting <- all_lg_scrape() %>%
mutate(player=case_when(
(player == "Charles Williams" & season == 1968)~"Charlie Williams",
TRUE~player)) %>%
left_join(.,psi) %>%
mutate(tm = ifelse(tm == "TOT", "1TOT", tm)) %>%
group_by(player_id,season,type) %>%
arrange(tm) %>%
slice(1) %>%
mutate(tm = ifelse(tm == "1TOT", "TOT", tm)) %>% ungroup() %>%
arrange(desc(season), type, number_tm) %>% select(-c(birth_year:experience))
alldef <- all_def_or_all_rookie("all_defense")
allrook <- all_def_or_all_rookie("all_rookie")
end_seas_teams <- bind_rows(all_lg_without_voting %>% select(-c(seas_id,player_id)), alldef, allrook) %>%
mutate(player=case_when(
(player == "Jaren Jackson" & season >=2019)~"Jaren Jackson Jr.",
(player == "Marvin Bagley" & season >= 2019)~"Marvin Bagley III",
(player == "Dennis Smith" & season >= 2018)~"Dennis Smith Jr.",
(player == "Taurean Waller-Prince" & season >= 2018)~"Taurean Prince",
(player == "Tim Hardaway" & season >= 2014)~"Tim Hardaway Jr.",
(player == "Nenê Hilário" & season >= 2003)~"Nenê",
(player == "Michael Porter" & season >= 2020)~"Michael Porter Jr.",
(player == "Jabari Smith" & season>=2023)~"Jabari Smith Jr.",
(player == "Jaime Jaquez" & season>=2024)~"Jaime Jaquez Jr.",
(player == "Dereck Lively" & season>=2024)~"Dereck Lively II",
(player == "Gregory Jackson" & season>=2024)~"GG Jackson II",
TRUE~player
)) %>% left_join(., psi) %>%
select(season:birth_year, tm, age) %>%
# two George Johnsons played at same time, one won All-Defense, remove the other
filter(!(player == "George Johnson" & birth_year == 1956)) %>%
mutate(tm = ifelse(tm == "TOT", "1TOT", tm)) %>%
group_by(season,lg,type,number_tm,player,position) %>%
arrange(tm) %>%
slice(1) %>%
mutate(tm = ifelse(tm == "1TOT", "TOT", tm)) %>% ungroup() %>%
arrange(desc(season), type, number_tm, player)
write_csv(end_seas_teams, "Data/End of Season Teams.csv")
all_stars_all_years <- tibble()
# nba (no game in 1999)
sapply(setdiff(current_year:1951, 1999), function(x) {
new_seas <- all_stars(x)
all_stars_all_years <<- rbind(all_stars_all_years, new_seas)
print(x)
})
# aba
sapply(1976:1968, function(x) {
new_seas <- all_stars(x, "ABA")
all_stars_all_years <<- rbind(all_stars_all_years, new_seas)
print(x)
})
all_stars_cleaned <- all_stars_all_years %>%
mutate(
replaced = str_detect(player, "\\("),
player = ifelse(replaced == TRUE, substr(player, 1, nchar(player) - 4), player),
hof = str_detect(player, "\\*"),
player = ifelse(hof == TRUE, substr(player, 1, nchar(player) - 1), player)
) %>% select(-hof) %>% arrange(desc(season),lg,team)
write_csv(all_stars_cleaned, "Data/All-Star Selections.csv")