-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolution.Rmd
168 lines (138 loc) · 3.8 KB
/
solution.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
---
title: 'Applied R Munich: "A Grammar of Data Manipulation" -- Eine Einführung in das
Paket dplyr <br> __Solution__'
author: "Philipp J. Rösch"
date: "26.10.2015"
output: html_document
---
<br>
Die Fragen stammen größtenteils aus Hadley Wickhams [dplyr-Tutorial](https://www.dropbox.com/sh/i8qnluwmuieicxc/AAAgt9tIKoIm7WZKIyK25lh6a) von der useR! 2014. Danke hierfür!
```{r}
suppressMessages(library(dplyr))
library(hflights)
flights <- tbl_df(hflights)
```
#### 1. Welcher Flug hat am meisten Verspätung aufgeholt? Verwende `FlightNum`, `DepDelay` und `ArrDelay`.
```{r}
flights %>%
select(FlightNum, DepDelay, ArrDelay) %>%
mutate(caught_up = desc(DepDelay - ArrDelay)) %>%
arrange(caught_up)
```
#### 2. Berechne die Geschwindigkeit in mph mit `AirTime` (in Minuten) und `Distance` (in miles). Erstelle außerdem die Variable Geschwindigkeit in km/h. Welche Flugzeuge (`FlightNum`) sind am schnellsten?
```{r}
flights2 <- flights %>%
select(UniqueCarrier, FlightNum, AirTime, Distance) %>%
mutate(mph = Distance / AirTime * 60) %>%
mutate(kmh = mph * 1.60934) %>%
arrange(desc(kmh)) %>%
print
```
Zusatzfrage: Erstelle eine Häufigkeitstabelle der Airlines für die 20 schnellsten Flüge.
```{r}
flights2 %>%
filter(min_rank(desc(kmh)) <=20) %>%
select(UniqueCarrier) %>%
table()
```
oder:
```{r}
flights2 %>%
top_n(20) %>%
select(UniqueCarrier) %>%
table()
```
#### 3. Welche Airline ist im Durchschnitt am schnellsten?
```{r}
flights2 %>%
group_by(UniqueCarrier) %>%
summarise(carrier_avg_speed = mean(kmh, na.rm = TRUE)) %>%
arrange(desc(carrier_avg_speed))
```
oder:
```{r}
flights2 %>%
filter(!is.na(kmh)) %>%
group_by(UniqueCarrier) %>%
summarise(carrier_avg_speed = mean(kmh)) %>%
arrange(desc(carrier_avg_speed))
```
#### 4. Schreibe den unten stehenden Code in die Chaining-Syntax um.
```{r eval=FALSE}
flights4 <- flights %>%
mutate(hour = DepTime %/% 100, date = sprintf("%04s-%02s-%02s", Year, Month, DayofMonth))
hourly_delay <- filter(
summarise(
group_by(
filter(
flights4, !is.na(DepDelay)
),
date, hour
),
avg_delay = mean(DepDelay),
n = n()
),
n > 10
)
```
```{r}
flights %>%
mutate(hour = DepTime %/% 100, date = sprintf("%04s-%02s-%02s", Year, Month, DayofMonth)) %>%
filter(!is.na(DepDelay)) %>%
group_by(date, hour) %>%
summarise(delay = mean(DepDelay), n = n()) %>%
filter(n > 10)
```
#### 5. Um wie viel Uhr starten jeweils täglich die ersten Flieger vom George Bush Intercontinental Airport (IAH)? `DepTime` ist hier ein Integer.
```{r}
flights %>%
filter(Origin == "IAH") %>%
select(Month, DayofMonth, UniqueCarrier, DepTime) %>%
group_by(Month, DayofMonth) %>%
summarise(first_flight = min(DepTime, na.rm = TRUE))
```
#### 6. Gebe für jedes Flugzeug die zwei Flüge mit der meisten Verspätung aus. Was ist hier der Unterschied zwischen `min_rank`, `row_number` und `dense_rank`?
```{r}
x <- c(1, 1, 2, 3, 5)
min_rank(x)
row_number(x)
dense_rank(x)
```
```{r}
# with min_rank
flights %>%
select(ArrDelay, FlightNum) %>%
filter(!is.na(ArrDelay)) %>%
group_by(FlightNum) %>%
filter(min_rank(desc(ArrDelay)) <=2) %>%
arrange(FlightNum) %>%
print() %>%
summarise(n = n()) %>%
group_by(n) %>%
summarise(N = n())
# use top_n
flights %>%
select(FlightNum, ArrDelay) %>%
filter(!is.na(ArrDelay)) %>%
group_by(FlightNum) %>%
top_n(2) %>%
arrange(FlightNum)
# with row_number
flights %>%
select(ArrDelay, FlightNum) %>%
filter(!is.na(ArrDelay)) %>%
group_by(FlightNum) %>%
filter(row_number(desc(ArrDelay)) <=2) %>%
arrange(FlightNum) %>%
print() %>%
summarise(n = n()) %>%
group_by(n) %>%
summarise(N = n())
```
Zusatzaufgabe: Welche Flüge gab es bloß einmal in 2011?
```{r}
flights %>%
group_by(FlightNum) %>%
summarise(n = n()) %>%
filter(n == 1)
```