-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpractical_3.Rmd
executable file
·208 lines (147 loc) · 4.03 KB
/
practical_3.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
---
title: "Practical_3"
author: "Pietro Franceschi"
date: "03/10/2019"
output: html_document
---
## Loads
```{r}
## libraries
library(tidyverse)
library(readr)
```
```{r}
## Olympic games data
athl <- read_csv("data/athlete_events.csv")
```
How many specialities so we have?
```{r}
## number of specialities
length(unique(athl$Event))
## number of athlets
nrow(athl)
```
```{r}
length(unique(athl$Name))
```
In athletics, can we see a change in the age of the athlets over time?
```{r}
## let's get the list of the sports
unique(athl$Sport)
```
```{r}
athl %>%
filter(Sport == "Athletics") %>%
ggplot() +
geom_jitter(aes(x = Year, y = Age, col = Sex), width = 0.2, size = 0.1) +
scale_color_brewer(palette = "Set1", name = "Gender") +
facet_wrap(~Sex, ncol = 1) +
theme_light()
```
Which, by the way, gives already some interesting result ... apparently the age of participation is rising mainly in females
```{r}
athl %>%
filter(Sport == "Athletics") %>%
filter(Medal %in% c("Gold","Silver","Bronze")) %>%
ggplot() +
geom_jitter(aes(x = Year, y = Age, col = Sex), width = 0.2, size = 0.1) +
geom_smooth(aes(x = Year, y = Age), method = lm) +
scale_color_brewer(palette = "Set1", name = "Gender") +
facet_wrap(~Sex, ncol = 1) +
theme_light()
```
## Grouping and Summarising
As an easy starting point we start from the iris dataset
```{r}
## get the data
data(iris)
```
Now we group it ...
```{r}
iris %>%
group_by(Species)
```
Apparently nothing has changed ...
```{r}
## This performs "on the fly" the mean per group!
mytable <- iris %>%
group_by(Species) %>%
summarise(sep = mean(Sepal.Length))
## remember if we not assign the pipe to something the output will be only printed!
```
```{r}
## This performs "on the fly" the mean and sample counts per group!
iris %>%
group_by(Species) %>%
summarise(sep = mean(Sepal.Length),
nsamp = length(Sepal.Length))
```
Multiple columns can be summarised by using `summarise_at`
```{r}
iris %>%
group_by(Species) %>%
summarise_at(vars(`Sepal.Length`:`Petal.Width`),
.funs = list(sep = mean,
nsamp = length))
```
This type of writing ca be challenging ... the alternative is to rely on pivot_longer ;-)
```{r}
iris %>%
pivot_longer(-Species, names_to = "parameter", values_to = "value") %>%
group_by(parameter,Species) %>%
summarise(mean = mean(value),
sd = sd(value))
```
Or, even more intuitive, within "summarise" keep adding columns...
```{r}
iris %>%
group_by(Species) %>%
summarise(mean.sl=mean(Sepal.Length), mean.sw=mean(Sepal.Width))
```
Rank all athletes based on the # of gold metals
```{r}
athl %>%
filter(Season == "Summer") %>%
filter(Medal %in% c("Gold")) %>%
group_by(Year,Name,Sport) %>%
summarise(medals = length(Medal)) %>%
ungroup() %>%
filter(medals > 3) %>%
arrange(desc(medals))
```
Plotting the average age for the athlets over the years ...
```{r}
athl %>%
filter(Sport == "Athletics") %>%
filter(Sex == "M") %>%
group_by(Year) %>%
summarise(avg_age = mean(Age, na.rm = TRUE)) %>%
ungroup() %>%
ggplot() +
geom_line(aes(x = Year, y = avg_age), col = "steelblue") +
geom_point(aes(x = Year, y = avg_age), col = "orange", alpha = 0.5, size = 3) +
ylab("Average Athlete Age") +
theme_light()
```
## Code Hints for Assignment #9
```{r}
## If you calculate the mean of a vector containing NAs, you will get NA by default
## to exclude the NAs you have to add an argument to the mean() function
mean(c(1,4,5,6,NA), na.rm = TRUE)
```
```{r}
## The list of events is quite long ... to get the "right" name for an event you need
eventlist <- unique(athl$Event)
length(eventlist)
```
```{r}
## the function grep("string",vector) tells you the index of the positions where "string"
## is present in the vector
id100 <- grep("400",eventlist)
id100
## prints the event names which contains "400"
eventlist[id100]
```
```{r}
focusevent <- c("Athletics Men's 400 metres", "Athletics Men's 200 metres")
```