-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathch13.Rmd
228 lines (162 loc) · 5.96 KB
/
ch13.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
---
title: "Ch13"
output:
html_document:
df_print: paged
---
```{r}
library(tidyverse)
library(lubridate)
```
## Exercises 16.2.4
What happens if you parse a string that contains invalid dates?
```{r}
ymd(c("2010-10-10", "bananas"))
```
It retuns the parsed object as a Date object (so it parses it) but returns the failed string as NA.
What does the tzone argument to today() do? Why is it important?
```{r}
# It gives the date of the specific time zone. If you're in China, the date might be different than from the U.S at a given moment.
# Check OlsonNames()
# for al TZ names
tz <- str_subset(OlsonNames(), "Madrid")
today(tzone = tz)
```
Use the appropriate lubridate function to parse each of the following dates:
```{r}
d1 <- "January 1, 2010"
mdy(d1)
d2 <- "2015-Mar-07"
ymd(d2)
d3 <- "06-Jun-2017"
dmy(d3)
d4 <- c("August 19 (2015)", "July 1 (2015)")
mdy(d4)
d5 <- "12/30/14" # Dec 30, 2014
mdy(d5)
```
## Exercises 16.3.4
How does the distribution of flight times within a day change over the course of the year?
```{r}
flights_dt %>%
mutate(time = hour(dep_time) * 100 + minute(dep_time),
mon = as.factor(month
(dep_time))) %>%
ggplot(aes(x = time, y = ..density.., group = mon, color = mon)) +
geom_freqpoly(binwidth = 100)
```
Taken from: https://jrnold.github.io/r4ds-exercise-solutions/dates-and-times.html#time-spans
Compare dep_time, sched_dep_time and dep_delay. Are they consistent? Explain your findings.
```{r}
flights_dt %>%
select(dep_time, sched_dep_time, dep_delay) %>%
mutate(other_dep_time = (dep_time - sched_dep_time) / 60) %>%
filter(dep_delay != other_dep_time) %>%
View()
```
There seems to be a discrepancy for flights that flew from one day to another.
Compare air_time with the duration between the departure and arrival. Explain your findings. (Hint: consider the location of the airport.)
```{r}
flights_dt %>%
mutate(air_time2 = as.numeric(arr_time - dep_time),
diff = air_time2 - air_time) %>%
select(dep_time, arr_time, air_time, air_time2, diff)
```
How does the average delay time change over the course of a day? Should you use dep_time or sched_dep_time? Why?
```{r}
flights_dt %>%
group_by(minute = minute(dep_time)) %>%
summarize(avg_delay = mean(dep_delay)) %>%
ggplot(aes(minute, avg_delay)) +
geom_line()
flights_dt %>%
group_by(minute = minute(sched_dep_time)) %>%
summarize(avg_delay = mean(dep_delay)) %>%
ggplot(aes(minute, avg_delay)) +
geom_line()
```
`dep_time` because it shows the real pattern of delays. The scheduled departure time shows no pattern over the day.
On what day of the week should you leave if you want to minimise the chance of a delay?
```{r}
flights_dt %>%
group_by(day = wday(dep_time, label = TRUE, week_start = 1)) %>%
summarize(avg_delay = mean(dep_delay)) %>%
ggplot(aes(day, avg_delay)) +
geom_line(aes(group = 1)) +
geom_point()
```
Saturday!
What makes the distribution of diamonds$carat and flights$sched_dep_time similar?
```{r}
ggplot(diamonds, aes(carat %% 1 * 100)) +
geom_histogram(bins = 100)
```
```{r}
ggplot(flights_dt, aes(minute(sched_dep_time))) +
geom_histogram(bins = 100)
```
Taken from: https://jrnold.github.io/r4ds-exercise-solutions/dates-and-times.html#time-spans
Confirm my hypothesis that the early departures of flights in minutes 20-30 and 50-60 are caused by scheduled flights that leave early. Hint: create a binary variable that tells you whether or not a flight was delayed.
Here we calculate the percentage of flights that left early every minute.
```{r}
flights_dt %>%
transmute(early = dep_delay < 0,
minute = minute(sched_dep_time)) %>%
group_by(minute) %>%
summarise(early = mean(early)) %>%
ggplot(aes(x = minute, y = early)) +
geom_point()
```
We can check it out by groups of minutes
```{r}
flights_dt %>%
transmute(early = dep_delay < 0,
minute = minute(sched_dep_time),
group_minute = cut(minute, 6)) %>%
group_by(group_minute) %>%
summarise(early = mean(early)) %>%
ggplot(aes(x = group_minute, y = early)) +
geom_point()
```
Flights that were suppose to leave in the first ten minutes had greater chances of leaving early than flights which were supposed to leave in the last 20 minutes of the hour.
There's a problem with flights that flew over night, let's fix it.
```{r}
flights_dt <-
flights_dt %>%
mutate(
overnight = arr_time < dep_time,
arr_time = arr_time + days(overnight * 1), # I don't know what * 1 is for
sched_arr_time = sched_arr_time + days(overnight * 1)
)
```
## Exercises 16.4.5
Why is there months() but no dmonths()?
Because durations are expressed in seconds and different months have different seconds. For example, February has less days depending on the year. It would be different if you specify the year and month.
Explain days(overnight * 1) to someone who has just started learning R. How does it work?
```{r}
overnight <- c(TRUE, FALSE, FALSE, TRUE, NA)
days(overnight * 1)
```
Imagine you have the date 1st of March 2012. You'd like to know the date 100 days in the future. The first date is `dmy(1032012)`. `days(100)` creates a date object of `100` days that you can add to the first date, like `dmy(1032012) + days(100)`.
`overnight` is a vector or logicals, so it will create either 1 day objects or 0 days objects (nothing). I don't know what the 1 is for.
Create a vector of dates giving the first day of every month in 2015. Create a vector of dates giving the first day of every month in the current year.
```{r}
ymd(20150101) + months(1:12)
```
for the second
```{r}
floor_date(today(), unit = "year") + months(1:12)
```
Write a function that given your birthday (as a date), returns how old you are in years.
```{r}
my_bd <- function(birthday) {
as.numeric(as.duration(today() - birthday), "years") %/% 1
}
birthday <- ymd(19910301)
my_bd(birthday)
```
Why can’t (today() %--% (today() + years(1)) / months(1) work?
```{r}
today() %--% (today() + years(1)) / months(1)
```
It works :/