Use dev (soon to be released) lubridate

This commit is contained in:
hadley 2016-08-18 10:03:38 -05:00
parent d071a1a45c
commit 39d074e706
2 changed files with 7 additions and 13 deletions

View File

@ -39,6 +39,7 @@ Imports:
viridis
Remotes:
hadley/forcats,
hadley/lubridate,
hadley/modelr,
hadley/stringr,
hadley/tibble,

View File

@ -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)
```