-
Notifications
You must be signed in to change notification settings - Fork 1
/
dvoa_expected_wins.R
48 lines (39 loc) · 1.38 KB
/
dvoa_expected_wins.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
library(tidyverse)
library(rvest)
dvoa_url_for_year <- function(year) {
str_interp("https://www.footballoutsiders.com/stats/nfl/team-efficiency/${year}")
}
dvoa_local_filename_for_year <- function(year) {
str_interp("data/${year}/dvoa.html")
}
dvoa_download_file_for_year <- function(year) {
url <- dvoa_url_for_year(year)
local_filename <- dvoa_local_filename_for_year(year)
if (!file.exists(local_filename)) {
download.file(url, local_filename)
}
}
dvoa_load_data_for_year <- function(year) {
dvoa_download_file_for_year(year)
filename <- dvoa_local_filename_for_year(year)
file_contents <- read_html(filename)
nodes <- html_nodes(file_contents, "table")[[1]]
data <- html_table(nodes, fill = TRUE)
# Rename column since source splits it across two lines
colnames(data)[3] <- "TOTAL.DVOA"
# Column numbers and names changed in 2020
if (year >= 2020) {
colnames(data)[8] <- "OFFENSEDVOA"
colnames(data)[10] <- "DEFENSEDVOA"
colnames(data)[12] <- "S.T.DVOA"
}
data <- data %>%
select(-1) %>% # Drop unnecessary first column with numerical indexes
select(TEAM, TOTAL.DVOA, OFFENSEDVOA, DEFENSEDVOA, S.T.DVOA) %>%
mutate(TOTAL.DVOA=parse_number(TOTAL.DVOA),
OFFENSEDVOA=parse_number(OFFENSEDVOA),
DEFENSEDVOA=parse_number(DEFENSEDVOA),
S.T.DVOA=parse_number(S.T.DVOA),
Year = year)
return(data)
}