-
Notifications
You must be signed in to change notification settings - Fork 0
/
ETL.R
159 lines (122 loc) · 4.61 KB
/
ETL.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
library(rvest)
library(tidyverse)
library(stringi)
## scrape player data
getPlayerData <- function(){
url <- "http://onequestionshootout.xyz/players/series_all.htm"
players <- url %>%
xml2::read_html() %>%
html_nodes("table") %>%
.[2] %>%
html_table(fill = TRUE)
data <- players [[1]]
# clean player data
player_data <-data %>%
mutate(PlayerNo. = as.numeric(str_replace(PlayerNo., "P",""))) %>%
mutate(CashBuilder = parse_number(CashBuilder)) %>%
mutate(ChosenOffer = str_replace(ChosenOffer, "=", "")) %>%
mutate(ChosenOffer = str_replace(ChosenOffer, "/", "")) %>%
mutate(ChosenOffer = str_replace(ChosenOffer, "£", "")) %>%
mutate(ChosenOffer = gsub("([\\])", "", ChosenOffer) ) %>%
mutate(ChosenOffer = parse_number(ChosenOffer)) %>%
mutate(LowerOffer = str_replace(LowerOffer, "£", "")) %>%
mutate(LowerOffer = parse_number(LowerOffer)) %>%
mutate(HigherOffer = parse_number(HigherOffer)) %>%
mutate(HTHResult = parse_number(HTHResult)) %>%
mutate(FC_Correct_Answers_Player = stri_extract_first_regex(`FC CorrectAnswers`,"[0-9]+")) %>%
mutate(FC_team_size = case_when(grepl("solo", `FC CorrectAnswers`, fixed = TRUE) == TRUE ~ "1",
grepl("solo", `FC CorrectAnswers`, fixed = TRUE) == FALSE ~ stri_extract_last_regex(`FC CorrectAnswers`,"[0-9]+")
)
) %>%
mutate(
FC_Winner = case_when(
grepl("Chaser", `FC Winner`, fixed = TRUE) == TRUE ~ "Chaser",
grepl("Chaser", `FC Winner`, fixed = TRUE) == FALSE ~ "Team"
)
) %>%
mutate(
Team_Win_Amount = case_when(
FC_Winner == "Team" ~ stri_extract_last_regex(`FC Winner`,"[0-9]+")
)
) %>%
mutate(
Chaser_Win_Amount = case_when(
FC_Winner == "Chaser" ~ stri_extract_last_regex(`FC Winner`,"[0-9]:[0-9]+")
)
) %>%
mutate(
Special_round = case_when(
grepl("✝", `FC CorrectAnswers`, fixed = TRUE) == TRUE ~ TRUE,
grepl("✝", `FC CorrectAnswers`, fixed = TRUE) == FALSE ~ FALSE,
)
) %>%
mutate(`Amount WonBy Player`= parse_number(`Amount WonBy Player`)) %>%
mutate(Date = as.Date(Date, "%d/%m/%Y")) %>%
select(-c(`FC CorrectAnswers`,`FC Winner`,bgc)) %>%
rename(PlayerNumber = `PlayerNo.`,
FC_CorrectAnswers = FC_Correct_Answers_Player,
FC_TeamSize = FC_team_size,
TeamWinAmount = Team_Win_Amount,
ChaserWinAmount = Chaser_Win_Amount,
SpecialRound = Special_round,
HTH_Result = HTHResult)
# clean a data error
player_data$ChosenOffer[2073]=5000
return(player_data)
}
getEpisodeData <- function(){
url <- "http://onequestionshootout.xyz/episodes/series_all.htm"
episode <- url %>%
xml2::read_html() %>%
html_nodes("table") %>%
.[2] %>%
html_table(fill = TRUE)
data_episode <- episode [[1]]
data_episode <- data_episode %>%
separate(`Series &Episode`, c("Series","Episode")) %>%
mutate(PrizeFund = str_replace(PrizeFund, "£", "")) %>%
mutate(PrizeFund = parse_number(PrizeFund)) %>%
mutate(
FC_Winner = case_when(
grepl("Chaser", `Winner`, fixed = TRUE) == TRUE ~ "Chaser",
grepl("Chaser", `Winner`, fixed = TRUE) == FALSE ~ "Team"
)
) %>%
mutate(
Team_Win_Amount = case_when(
FC_Winner == "Team" ~ stri_extract_last_regex(`Winner`,"[0-9]+")
)
) %>%
mutate(
Chaser_Win_Amount = case_when(
FC_Winner == "Chaser" ~ stri_extract_last_regex(`Winner`,"[0-9]:[0-9]+")
)
) %>%
mutate(ChaserAccuracy = str_replace(ChaserAccuracy, "%", "")) %>%
mutate(ChaserAccuracy = as.numeric(ChaserAccuracy) / 100) %>%
# mark episode as special if it has asterix in chaser name
mutate(
SpecialEpisode = case_when(
grepl("*", `Chaser`, fixed = TRUE) == TRUE ~ TRUE,
TRUE ~ FALSE
)
)%>%
# Change asterixed Anne Hegerty episodes
mutate(
Chaser = replace(Chaser, Chaser == 'Anne Hegerty *', 'Anne Hegerty')
) %>%
# Separate Target Column
tidyr::separate("Target", into = c("InitialTarget", "InitialPushbacks"), sep = "\\+" ) %>%
mutate(InitialTarget = as.numeric(InitialTarget)) %>%
tidyr::replace_na(list(InitialPushbacks = 0)) %>%
mutate(InitialPushbacks = as.numeric(InitialPushbacks)) %>%
mutate(InitialTotal = InitialTarget + InitialPushbacks, .before = InitialTarget) %>%
select(-c(`Winner`,`FinalChaseVideo`)) %>%
# change datatypes
mutate(
Date = as.Date(Date, "%d/%m/%Y"),
Chaser = as.factor(Chaser),
FC_Winner = as.factor(FC_Winner)
)
return(data_episode)
}