Chapter 16 Dates and Times
library(tidyverse)
library(lubridate)
Attaching package: ‘lubridate’
The following object is masked from ‘package:igraph’:
%--%
The following object is masked from ‘package:base’:
date
library(nycflights13)
today()
[1] "2018-12-03"
now()
[1] "2018-12-03 10:23:15 PST"
Dates from strings
ymd("2017-01-31")
[1] "2017-01-31"
mdy("January 31st, 2017")
[1] "2017-01-31"
dmy("31-Jan-2017")
[1] "2017-01-31"
Dates with times
ymd_hms("2017-01-31 20:11:59")
[1] "2017-01-31 20:11:59 UTC"
mdy_hm("01/31/2017 08:01")
[1] "2017-01-31 08:01:00 UTC"
flights %>%
select(year, month, day, hour, minute)
flights %>%
select(year, month, day, hour, minute) %>%
mutate(departure = make_datetime(year, month, day, hour, minute))
make_datetime_100 <- function(year, month, day, time) {
make_datetime(year, month, day, time %/% 100, time %% 100)
}
flights_dt <- flights %>%
filter(!is.na(dep_time), !is.na(arr_time)) %>%
mutate(
dep_time = make_datetime_100(year, month, day, dep_time),
arr_time = make_datetime_100(year, month, day, arr_time),
sched_dep_time = make_datetime_100(year, month, day, sched_dep_time),
sched_arr_time = make_datetime_100(year, month, day, sched_arr_time)
) %>%
select(origin, dest, ends_with("delay"), ends_with("time"))
flights_dt
flights_dt %>%
ggplot(aes(dep_time)) +
geom_freqpoly(binwidth = 86400) # 86400 seconds = 1 day
flights_dt %>%
filter(dep_time < ymd(20130102)) %>%
ggplot(aes(dep_time)) +
geom_freqpoly(binwidth = 600) # 600 s = 10 minutes
Getting the components
datetime <- ymd_hms("2016-07-08 12:34:56")
year(datetime)
[1] 2016
month(datetime)
[1] 7
mday(datetime)
[1] 8
yday(datetime)
[1] 190
wday(datetime)
[1] 6
flights_dt %>%
mutate(wday = wday(dep_time, label = TRUE)) %>%
ggplot(aes(x = wday)) +
geom_bar()
flights_dt %>%
mutate(minute = minute(dep_time)) %>%
group_by(minute) %>%
summarise(
avg_delay = mean(arr_delay, na.rm = TRUE),
n = n()) %>%
ggplot(aes(minute, avg_delay)) +
geom_line()
sched_dep <- flights_dt %>%
mutate(minute = minute(sched_dep_time)) %>%
group_by(minute) %>%
summarise(
avg_delay = mean(arr_delay, na.rm = TRUE),
n = n())
ggplot(sched_dep, aes(minute, avg_delay)) +
geom_line()
Time zones
Sys.timezone()
[1] "America/Los_Angeles"
All of the time zones
length(OlsonNames())
[1] 606
head(OlsonNames())
[1] "Africa/Abidjan" "Africa/Accra" "Africa/Addis_Ababa" "Africa/Algiers" "Africa/Asmara"
[6] "Africa/Asmera"
(x1 <- ymd_hms("2015-06-01 12:00:00", tz = "America/New_York"))
[1] "2015-06-01 12:00:00 EDT"
(x2 <- ymd_hms("2015-06-01 18:00:00", tz = "Europe/Copenhagen"))
[1] "2015-06-01 18:00:00 CEST"
(x3 <- ymd_hms("2015-06-02 04:00:00", tz = "Pacific/Auckland"))
[1] "2015-06-02 04:00:00 NZST"
LS0tCnRpdGxlOiAiU3RhdCA0NTAgRGF0ZXMgYW5kIFRpbWVzIgphdXRob3I6ICJQcm9mLiBFcmljIEEuIFN1ZXNzIgpkYXRlOiAiRGVjZW1iZXIgMywgMjAxOCIKb3V0cHV0OgogIHdvcmRfZG9jdW1lbnQ6IGRlZmF1bHQKICBwZGZfZG9jdW1lbnQ6IGRlZmF1bHQKICBodG1sX25vdGVib29rOiBkZWZhdWx0Ci0tLQoKIyBDaGFwdGVyIDE2IERhdGVzIGFuZCBUaW1lcwoKYGBge3J9CmxpYnJhcnkodGlkeXZlcnNlKQoKbGlicmFyeShsdWJyaWRhdGUpCmxpYnJhcnkobnljZmxpZ2h0czEzKQpgYGAKCmBgYHtyfQp0b2RheSgpCgpub3coKQpgYGAKCkRhdGVzIGZyb20gc3RyaW5ncwoKYGBge3J9CnltZCgiMjAxNy0wMS0zMSIpCgptZHkoIkphbnVhcnkgMzFzdCwgMjAxNyIpCgpkbXkoIjMxLUphbi0yMDE3IikKYGBgCgpEYXRlcyB3aXRoIHRpbWVzCgpgYGB7cn0KeW1kX2htcygiMjAxNy0wMS0zMSAyMDoxMT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