diff --git a/DESCRIPTION b/DESCRIPTION index b0ff624..8773a5a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -39,6 +39,7 @@ Imports: viridis Remotes: hadley/forcats, + hadley/lubridate, hadley/modelr, hadley/stringr, hadley/tibble, diff --git a/datetimes.Rmd b/datetimes.Rmd index e393139..007b852 100644 --- a/datetimes.Rmd +++ b/datetimes.Rmd @@ -137,34 +137,27 @@ Or within a single day: ```{r} flights_dt %>% - filter(dep_time < ymd(20130102, tz = "UTC")) %>% + filter(dep_time < ymd(20130102)) %>% ggplot(aes(dep_time)) + geom_freqpoly(binwidth = 600) # 600 s = 10 minutes ``` -Note the two tricks I needed to create these plots: - -1. When you use date-times in a numeric context (like in a histogram), 1 - means 1 second, so a binwidth of 86400 means one day. For dates, 1 - means 1 day. - -1. R doesn't like to compare date-times with dates, so you can force - `ymd()` to generate a date-time by supplying a `tz` argument. +Note that when you use date-times in a numeric context (like in a histogram), 1 means 1 second, so a binwidth of 86400 means one day. For dates, 1 means 1 day. ### From other types You may want to switch between a date-time and a date. That's the job of `as_datetime()` and `as_date()`: ```{r} -# as_datetime(today()) +as_datetime(today()) as_date(now()) ``` Sometimes you'll get date/times as numeric offsets from the "Unix Epoch", 1970-01-01. If the offset is in seconds, use `as_datetime()`; if it's in days, use `as_date()`. ```{r} -# as_datetime(60 * 60 * 10) -as_date(365) +as_datetime(60 * 60 * 10) +as_date(365 * 10 + 2) ``` ### Exercises @@ -302,7 +295,7 @@ You can use `update()` to show the distribution of flights across the course of ```{r} flights_dt %>% - mutate(dep_hour = update(dep_time, month = 1, day = 1)) %>% + mutate(dep_hour = update(dep_time, yday = 1)) %>% ggplot(aes(dep_hour)) + geom_freqpoly(binwidth = 300) ```