class: title-slide, center, bottom # Getting and setting dates and times with <img src="images/lubridate_hex.png" width="250" style="display: block; margin: auto;" /> ## RStudio Instructor Exam ### Dr. Laurie Baker ### `r Sys.Date()` --- # You * Are familiar with R, Rmarkdown notebooks, and dplyr. * Are new to working with dates and times in R * Want to learn easy ways to extract information from dates and times. <img src="images/lubridate.png" width="350" style="display: block; margin: auto;" /> Artwork by @allison_horst --- # Learning objectives * Learn to create date, time, and date-time objects with **helpers** (e.g. ymd, dmy_hms). * Learn to extract components (e.g. year, month, wday) from date-time objects with **accessors**. * Learn how to change time zones with `with_tz` --- # Getting started * For this adventure you'll need the `tidyverse` meta-package, `lubridate` (part of the tidyverse). We will also need the package `readxl` ```r #install.packages("tidyverse") #install.packages("lubridate") #install.packages("readxl") library(tidyverse) library(lubridate) library(readxl) ``` * And we'll be working with the dataframe `training_schedule.xlsx` which you might find familiar... --- layout: false class: center middle # What makes dates and times so challenging? --- # Physical phenomena <a title="Elf Pavlik / CC0" href="https://commons.wikimedia.org/wiki/File:Blue_Marble_rotating.gif"><img width="512" alt="Blue Marble rotating" src="https://upload.wikimedia.org/wikipedia/commons/4/45/Blue_Marble_rotating.gif"></a> Elf Pavlik / CC0 https://commons.wikimedia.org/wiki/File:Blue_Marble_rotating.gif ??? * Rotation of the Earth * Earth's orbit around the sun --- # Geopolitical phenomena * Months, time zones, leap years, and DST * The Tzolk'in Mayan calendar had 260 days! ![\Guardian News Headline](images/eu_guardian.png) --- # Dates and times in R Type can be 1. A date (`<date>`) 2. A time (`<time>`) 3. A date-time (`<dttm>`) a date plus time. -- Can be built from * Strings: 09/10/2010 * Existing date/time objects * Date-time components (year = 1990, month = 12, day = 4) --- # Parsing dates and times using helpers <img src="images/lubridate_ymd.png" width="600" style="display: block; margin: auto;" /> Artwork by @allison_horst --- # Parsing dates and times using helpers * Identify the order in which the year (`y`), month (`m`), and day (`d`) appears in your dates. * Let's look at October 2, 2020. ```r ymd("20201002") ``` ``` ## [1] "2020-10-02" ``` -- ```r ___("10-02-2020") ``` --- # Parsing dates and times using helpers * For date-times, we can add an underscore and one of more `h` `m` and `s` to our parsing function. * What helper would we use for October 3, 2020 if it comes in this format? ```r ____("03/10/20 15:32:05") ``` -- Times are supplied in `UTC`, the coordinated universal time, unless we specify a time zone (tz). --- # A tale of 5 time zones... Let's take a look at our training schedule ``` ## # A tibble: 6 x 5 ## date start_time end_time topic type ## <dttm> <dbl> <dbl> <chr> <chr> ## 1 2020-09-18 00:00:00 14 16 Introduction to R workshop ## 2 2020-10-02 00:00:00 14 17 Data Wrangling in R training ## 3 2020-10-09 00:00:00 14 16 Data Wrangling in R workshop ## 4 2020-10-16 00:00:00 14 17 Data Visualisation in R training ## 5 2020-10-23 00:00:00 14 16 Data Visualisation in R workshop ## 6 2020-10-30 00:00:00 14 16 Show and Tell presentation ``` ```r path <- here::here("../data","training_schedule.xlsx") schedule <- read_xlsx(path) schedule <- schedule %>% mutate(date = as_datetime(date)) head(schedule) ``` --- # Getting components We can pull out individual parts of a date with the **accessor** functions: - year, month, day - mday, yday, wday - hour, minute, second ```r schedule %>% mutate(year = year(date)) %>% head() ``` ``` ## # A tibble: 6 x 6 ## date start_time end_time topic type year ## <dttm> <dbl> <dbl> <chr> <chr> <dbl> ## 1 2020-09-18 00:00:00 14 16 Introduction to R workshop 2020 ## 2 2020-10-02 00:00:00 14 17 Data Wrangling in R training 2020 ## 3 2020-10-09 00:00:00 14 16 Data Wrangling in R workshop 2020 ## 4 2020-10-16 00:00:00 14 17 Data Visualisation i~ training 2020 ## 5 2020-10-23 00:00:00 14 16 Data Visualisation i~ workshop 2020 ## 6 2020-10-30 00:00:00 14 16 Show and Tell presentat~ 2020 ``` --- # Getting components Fill in the blank to determine what day of the week our courses fall on ```r schedule %>% mutate(weekday = ____(date)) ``` We can change the arguments label = TRUE, abbr = FALSE. --- # Creating a date time from date-time components We can create a date-time by combining our date and start-time ```r schedule <- schedule %>% mutate( start_date_time = make_datetime( year = year(date), month = month(date), day = day(date), hour = start_time, tz = "Europe/London") ) ``` <!-- Try creating the `end_date_time` by filling in the blanks --> --- # Changing timezones with `with_tz` But what is the time in Jamaica? ```r schedule <- schedule %>% mutate(start_date_time_jam = with_tz( start_date_time, tz = "America/Jamaica")) ``` --- ![\Concept Map](images/concept_map.png) --- class: center, middle # Thanks! <img src="images/lubridate_hex.png" width="150" /> Slides created via the R package [**xaringan**](https://github.com/yihui/xaringan). The chakra comes from [remark.js](https://remarkjs.com), [**knitr**](http://yihui.name/knitr), and [R Markdown](https://rmarkdown.rstudio.com). The gorgeous artwork comes from [@allison_horst](https://github.com/allisonhorst/stats-illustrations) --- # Having some fun * Try changing the country to Belize Hint: Run `OlsonNames(tzdir = NULL)` to find the correct time zone ```r OlsonNames(tzdir = NULL) schedule <- schedule %>% mutate(start_date_time___ = with_tz( start_date_time, tz = "_____")) ``` Fill in the blanks to find out what time the delegates from Belize get to sleep in? ```r schedule %>% filter(______(start_date_time____) > 7) %>% select(start_date_time_bz) ``` <!-- --- --> <!-- # Calculating a duration --> <!-- How long is each training session and workshop? --> <!-- ```{r} --> <!-- schedule <- schedule %>% --> <!-- mutate(duration = interval(end_date_time, start_date_time)) --> <!-- ``` -->