-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths1-exercises-with-answers.Rmd
263 lines (173 loc) · 5.54 KB
/
s1-exercises-with-answers.Rmd
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
---
title: "Session 1 Exercises (with answers)"
author: "Ben Raymond, Adrien Ickowicz"
output:
html_document:
toc: false
theme: flatly
highlight: zenburn
css: "extra/exercises.css"
---
```{r chunkopts, eval = TRUE, echo = FALSE}
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
options(width=80)
options(tibble.width = 80, tibble.print_max = 10, tibble.print_min = 10)
```
# Preparation
Load some packages:
```{r setup}
library(tidyverse)
library(lubridate)
```
And read in our data (set scores from the 2019 D1 Women's NCAA competition, scraped from the NCAA web site).
```{r data}
x <- read_csv("example_data/NCAA_D1W_2019.csv")
glimpse(x)
```
# Exercises
### 1. What is the date range of the matches?
<details>
<summary>Click for solution</summary>
```{r}
range(x$date)
```
</details>
### 2. How many matches were played?
<details>
<summary>Click for solution</summary>
```{r}
length(unique(x$match_id))
```
</details>
### 3. How many teams?
<details>
<summary>Click for solution</summary>
```{r}
length(unique(c(x$home_team, x$visiting_team)))
```
</details>
### 4. Count the number of matches that went to 3, 4, and 5 sets
<details>
<summary>Click for solution</summary>
```{r}
x %>% group_by(match_id) %>% slice(1) %>% ungroup %>% ## one row per match
count(match_n_sets) ## count the number of times each value of match_n_sets appears
## or
x %>% distinct(match_id, match_n_sets) %>%
count(match_n_sets)
```
</details>
### 5. On average, how many sets were played per match?
<details>
<summary>Click for solution</summary>
```{r}
x %>% group_by(match_id) %>% slice(1) %>% ungroup %>%
dplyr::summarize(mean_n_sets = mean(match_n_sets))
## or
nrow(x) / length(unique(x$match_id))
```
</details>
### 6. How many matches did Weber State play?
<details>
<summary>Click for solution</summary>
```{r}
x %>% filter(home_team == "Weber State" | visiting_team == "Weber State") %>%
summarize(n_matches = length(unique(match_id)))
```
or perhaps
```{r}
x %>% filter(home_team == "Weber State" | visiting_team == "Weber State") %>%
distinct(match_id) %>% nrow
```
or even
```{r}
length(unique(x$match_id[x$home_team == "Weber State" | x$visiting_team == "Weber State"]))
```
</details>
### 7. List the top 10 teams in terms of the most matches played
<details>
<summary>Click for solution</summary>
```{r}
bind_rows(x %>% distinct(match_id, team = home_team),
x %>% distinct(match_id, team = visiting_team)) %>%
count(team) %>% ## count the number of times each team name appears
dplyr::arrange(desc(n)) %>% ## arrange in decreasing count order
head(10) ## first 10 rows
```
</details>
### 8. Can you re-create the `margin` column, using the other columns in the data set?
<details>
<summary>Click for solution</summary>
```{r}
## add some extra columns
x <- x %>% mutate(margin = abs(home_team_score - visiting_team_score))
```
</details>
### 9. What was the average winning margin of a set?
<details>
<summary>Click for solution</summary>
```{r}
mean(x$margin)
```
</details>
### 10. Does that vary by set number 1--5?
<details>
<summary>Click for solution</summary>
```{r}
x %>% group_by(set_number) %>% dplyr::summarize(mean_winning_margin = mean(margin))
```
</details>
### 11. Plot a histogram of the score margin (all sets combined)
<details>
<summary>Click for solution</summary>
```{r}
ggplot(x, aes(x = margin)) + geom_histogram(binwidth = 1) +
labs(x = "Score margin", y = "Number of matches")
```
</details>
### 12. Same thing with each set shown separately
<details>
<summary>Click for solution</summary>
```{r}
ggplot(x, aes(x = margin)) + geom_histogram(binwidth = 1) +
facet_wrap(~set_number) +
labs(x = "Score margin", y = "Number of matches")
## or
ggplot(x, aes(x = margin, group = set_number, fill = as.factor(set_number))) +
geom_histogram(binwidth = 1, position = "dodge") + scale_fill_discrete(name = "Set number") +
labs(x = "Score margin", y = "Number of matches")
```
</details>
### 13. Find the most one-sided set result
<details>
<summary>Click for solution</summary>
```{r}
x %>% slice_max(margin)
## or
x[which(x$margin == max(x$margin)), ]
```
</details>
### 14. Is there a home team advantage? Find the match win rate for the home team (ignore the fact that some of these matches were played at neutral venues --- just take the `home_team` and `visiting_team` column names on face value)
<details>
<summary>Click for solution</summary>
```{r}
x <- x %>% group_by(match_id) %>% mutate(match_won_by = case_when(home_team_set_score > visiting_team_set_score ~ home_team,
TRUE ~ visiting_team)) %>%
ungroup
## but
x %>% dplyr::filter(match_won_by == visiting_team & visiting_team_set_score <= home_team_set_score)
## so
x <- x %>% group_by(match_id) %>% mutate(match_won_by = case_when(home_team_set_score > visiting_team_set_score ~ home_team,
visiting_team_set_score > home_team_set_score ~ visiting_team)) %>%
ungroup
x <- x %>% mutate(home_team_won_match = case_when(match_won_by == home_team ~ TRUE,
match_won_by == visiting_team ~ FALSE))
## and finally
x %>% group_by(match_id) %>% slice(1) %>% ungroup %>%
dplyr::summarize(p_home_win = mean(home_team_won_match, na.rm = TRUE))
## or
x %>% distinct(match_id, home_team_won_match) %>%
dplyr::summarize(p_home_win = mean(home_team_won_match, na.rm = TRUE))
```
</details>
<div id="footer"></div>