-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbk_ref_scraping.R
116 lines (100 loc) · 3.48 KB
/
bk_ref_scraping.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
install.packages("rvest")
install.packages("tidyverse")
install.packages("janitor")
install.packages("corrplot")
library(tidyverse) # for data wrangling
library(janitor) # for data cleaning
library(rvest) # for web scraping
library(corrplot) # correlation plots
scrape_stats <- function(season){
#scrape
#clean
return(player_stats)
}
url <- "https://www.basketball-reference.com/leagues/NBA_2017_totals.html"
stats <- url %>%
read_html() %>% #read html or xml
html_table() %>% #parse an html table into a dataframe
.[[1]]
str(stats)#str function provides summary of stats dataframe
attach(stats)
#All the data is character type. This is because the html table contains the header every 20 lines.
#So in the next step, we will get rid of this lines and also use the janitor package to do some basic
#cleaning such as fixing the column names. Additionally, we clean the data by turning the stats to numeric
#variables and NA to 0.
stats <- stats %>%
remove_empty_cols() %>% #if any exist
clean_names() %>% # all column names to lower case and removing "%"
dplyr::filter(!player=="Player") %>% #delete headers in data frame, :: is another way to reference a function
mutate_at(vars(-c(player,tm,pos)),as.numeric) %>% #turn all stat cols to numeric
mutate_at(vars(-c(player,tm,pos)), funs(replace(., is.na(.), 0))) %>% #turn NA to 0
as_tibble()
str(stats)
stats <- stats %>%
group_by(player) %>%
slice(1) %>%
ungroup()
#################
#Full Function
scrape_stats <- function(season = 2018){
#total stats
#scrape
url <- paste0("https://www.basketball-reference.com/leagues/NBA_",season,"_totals.html")
stats_tot <- url %>%
read_html() %>%
html_table() %>%
.[[1]]
#clean
player_stats_tot <- stats_tot %>%
remove_empty_cols() %>%
clean_names() %>%
dplyr::filter(!player=="Player") %>%
mutate_at(vars(-c(player,tm,pos)),as.numeric) %>%
mutate_at(vars(-c(player,tm,pos)), funs(replace(., is.na(.), 0))) %>%
as_tibble() %>%
group_by(player) %>%
slice(1) %>%
ungroup() %>%
select(-rk)
#per minute
url <- paste0("https://www.basketball-reference.com/leagues/NBA_",season,"_per_minute.html")
stats_pm <- url %>%
read_html() %>%
html_table() %>%
.[[1]]
player_stats_pm <- stats_pm %>%
remove_empty_cols() %>%
clean_names() %>%
dplyr::filter(!player=="Player") %>%
mutate_at(vars(-c(player,tm,pos)),as.numeric) %>%
mutate_at(vars(-c(player,tm,pos)), funs(replace(., is.na(.), 0))) %>%
as_tibble() %>%
group_by(player) %>%
slice(1) %>%
ungroup() %>%
rename_at(vars(9:29),funs(paste0(.,"_pm"))) %>%
select(-rk)
#advanced
url <- paste0("https://www.basketball-reference.com/leagues/NBA_",season,"_advanced.html")
stats_adv <- url %>%
read_html() %>%
html_table() %>%
.[[1]]
player_stats_adv <- stats_adv %>%
remove_empty_cols() %>%
clean_names() %>%
dplyr::filter(!player=="Player") %>%
mutate_at(vars(-c(player,tm,pos)),as.numeric) %>%
mutate_at(vars(-c(player,tm,pos)), funs(replace(., is.na(.), 0))) %>%
as_tibble() %>%
group_by(player) %>%
slice(1) %>%
ungroup() %>%
select(-rk)
player_stats <- full_join(player_stats_tot,player_stats_pm,
by = c("player", "pos", "age", "tm", "g", "gs", "mp")) %>%
full_join(player_stats_adv,
by = c("player", "pos", "age", "tm", "g", "mp"))
return(player_stats)
}
a <-scrape_stats(2018)