Skip to content

Commit

Permalink
Use dev (soon to be released) lubridate
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Aug 18, 2016
1 parent d071a1a commit 39d074e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Imports:
viridis
Remotes:
hadley/forcats,
hadley/lubridate,
hadley/modelr,
hadley/stringr,
hadley/tibble,
Expand Down
19 changes: 6 additions & 13 deletions datetimes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
```
Expand Down

0 comments on commit 39d074e

Please sign in to comment.