class: center, middle, inverse, title-slide # Date Night ## using lubridate to work with dates in R ### Jen Richmond ### 2021-12-10 --- # Useful links Code along...[https://rstudio.cloud/project/1817769](https://rstudio.cloud/project/1817769) Slides...[https://jennyslides.netlify.app/lubridate/](https://jennyslides.netlify.app/lubridate/) <img src="https://github.com/allisonhorst/stats-illustrations/raw/master/rstats-artwork/lubridate.png" width="300px" /> 🐦 @allisonhorst --- class: inverse, center, middle # The Plan -- ### an opinionated guide to learning new things in R -- ### 3 things I learned about lubridate -- ### how you can help others learn new things --- class: inverse, center, middle # an opinionated guide to learning new things in R --- # learning new things in R The best thing about open source software like R is that there are SO many learning resources. --- # learning new things in R The ~~best~~ worst thing about open source software like R is that there are SO many learning resources. -- ## too many choices... <iframe src="https://giphy.com/embed/ouE6OPO1MADM4" width="480" height="360" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/ouE6OPO1MADM4">via GIPHY</a></p> --- class: middle, centre <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/quad.png" width="900px" /> image credit: [Daniela Procida PyCon](https://www.youtube.com/watch?v=t4vKPhjcMZg&t=478s) ??? tutorial = learning oriented, lessons that take the reader by the hand through a series of steps to complete a project/exercise, show the beginner can achieve something meaningful how to guide = problem oriented, guides that take the reader through the steps required to solve a common problem, recipes/directions to do something specific reference = technical descriptions of the machinery and how to operate it discussion = explanations that clarify and illuminate a particular topic, background, context, explain why things are the way they are. --- class: middle, centre <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/prac_steps.png" width="900px" /> image credit: [Daniela Procida PyCon](https://www.youtube.com/watch?v=t4vKPhjcMZg&t=478s) --- class: middle, centre <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/theoretical.png" width="900px" /> image credit: [Daniela Procida PyCon](https://www.youtube.com/watch?v=t4vKPhjcMZg&t=478s) --- class: middle, centre <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/studying.png" width="900px" /> image credit: [Daniela Procida PyCon](https://www.youtube.com/watch?v=t4vKPhjcMZg&t=478s) --- class: middle, centre <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/coding.png" width="900px" /> image credit: [Daniela Procida PyCon](https://www.youtube.com/watch?v=t4vKPhjcMZg&t=478s) --- # in the context of R... .pull-left[ ### tutorials - books + [R for Data Science](https://r4ds.had.co.nz/) + [Learning Stats w R](https://learningstatisticswithr.com/) - courses + [RYouWithMe](www.rladiessydney.org/RYouWithMe) + [Data Science in a Box](https://datasciencebox.org/) - primers + [RStudio Cloud](https://rstudio.cloud/learn/primers) ] .pull-right[ ### how to guides - blogs + [Rebecca Barter purrr](http://www.rebeccabarter.com/blog/2019-08-19_purrr/) + [Alison Hill blogdown](https://alison.rbind.io/post/2017-06-12-up-and-running-with-blogdown/) + [Suzan Baert dpylr](https://suzan.rbind.io/categories/tutorial/) + [Jen Richmond IDHTG](http://jenrichmond.rbind.io/post/idhtg-how-to-use-colour-palettes-with-ggplot/) ] --- # in the context of R... .pull-left[ ### discussion - journal papers + [Hadley Wickham re tidyverse ](https://joss.theoj.org/papers/10.21105/joss.01686) + [Hadley Wickham re tidy data ](https://www.jstatsoft.org/article/view/v059i10) + [Jenny Bryan re version control ](https://peerj.com/preprints/3159/) + [Kara Woo re spreadsheets](https://www.tandfonline.com/doi/full/10.1080/00031305.2017.1375989) ] .pull-right[ ### reference - package documentation + CRAN docs + package vignette + cheat sheets ] --- class: middle, centre # my opinion skip tutorials and go straight to "how to" guides <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/table.png" width="900px" /> image credit: https://documentation.divio.com/introduction/ --- class: center, middle # why? # tutorials lack an interesting context/problem --- # without context/problem... <iframe src="https://giphy.com/embed/3o752onMtrE2FJJAru" width="480" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/cbc-funny-comedy-3o752onMtrE2FJJAru">via GIPHY</a></p> --- .pull-right[ <iframe src="https://giphy.com/embed/3ohhwAUoWvd7BeEkqA" width="480" height="478" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/cbc-funny-comedy-3ohhwAUoWvd7BeEkqA">via GIPHY</a></p> ] --- # how I learned lubridate # first google... <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/google.png" width="900px" /> --- #.... I hit GOLD .pull-left[ [Julia Silge blog](https://juliasilge.com/blog/lubridate-london-stage/) - great data set (London theatre from 1600) - solves common date problems + make characters into dates + plot by `year()`, `month()`, `wday()` + work with time spans ] .pull-right[ <img src="https://avatars0.githubusercontent.com/u/12505835?s=460&u=44b4e9026210eae8096439b1637898226050fdb1&v=4" width="400px" /> 🐦 @juliasilge ] --- # My learning plan .pull-left[ Find a cool dataset and copy Julia. dataset = [COVID testing rates NSW](https://data.nsw.gov.au/data/dataset/nsw-covid-19-tests-by-age-range/resource/28730d42-675b-4573-ad71-8156313c73a1) ] .pull-right[ SMH: Sept 15 2020 <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/smh.png" width="400px" /> ] --- class: inverse, center, middle # 3 things I learned about lubridate -- ### 1. plotting with date components -- ### 2. converting characters to dates -- ### 3. how to make R read your dates as dates --- class: inverse, center, middle ### 1. plotting with date components --- # date plots Use date components within `group_by()` + `summarise()` or within `ggplot()` using... .pull-left[ ### `year()` ### `month()` ### `day()` ### `wday()` ] .pull-right[ <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/plot.png" width="500px" /> ] --- class: center, middle Let's code... jump into [RStudio Cloud](https://rstudio.cloud/project/1817769) https://rstudio.cloud/project/1817769 --- class: inverse, center, middle ### 2. converting characters to dates --- # So R thinks your dates are characters... If your dates are in a consistent format, you can use... .pull-left[ ### `ymd()` ### `mdy()` ### `dmy()` ... to convert characters to dates ] --- class: center, middle Let's code... jump into [RStudio Cloud](https://rstudio.cloud/project/1817769) https://rstudio.cloud/project/1817769 --- class: inverse, center, middle ### 3. how to make R read your dates as dates --- # if only it were consistent... .pull-left[ Why did the covid testing data parse as dates... <img src="https://www.fairfieldcity.nsw.gov.au/files/assets/public/images/news/covid_drive_through_17.jpg?w=1200" width="400px" /> ] -- .pull-right[ ... but the Taylor/Beyonce data did not? <img src="https://akns-images.eonline.com/eol_images/Entire_Site/2019722/rs_600x600-190822114626-600.beyonce-taylor-swift-2009-mtv-vmas.ct.082219.jpg?fit=around|1200:1200&output-quality=90&crop=1200:1200;center,top" width="400px" /> ] --- ```r sales <- read_csv(here::here("data", "sales.csv")) sales$released[[1]] ``` ``` ## [1] "October 24, 2006" ``` ```r tests <- read_csv(here::here("data", "totaltests.csv")) tests$test_date[[1]] ``` ``` ## [1] "2020-03-09" ``` # my hypothesis Dates that are in year-month-day format (ISO 8601) will always parse as dates --- # collecting data to test my hypothesis .pull-left[ google form: enter your birthday 3 times - as you like - using a calendar widget - in year-month-day format (ISO 8601) ] .pull-right[ <img src="/Users/jenny/Desktop/Desktop - N591/git/slides/img/date_data.png" width="400px" /> ] --- # Which dates does R think are dates? ### it depends.... --- #### using googlesheets4 package - free date = list - template date = date 😄 - iso date = char .pull-left[ #### from .xlsx - free date = char - template date = date 😄 - iso date = char ] -- .pull-right[ #### from .csv - free date = char - template date = char - iso date = date 😠 ] --- # what to do with dates in a million formats? ```r diff_dates <- read_csv(here::here("data", "diffdatedata.csv")) %>% select(free_date) head(diff_dates) ``` ``` ## # A tibble: 6 × 1 ## free_date ## <chr> ## 1 30 June 1978 ## 2 17 Feb, 2011 ## 3 March 25 2014 ## 4 1977/7/11 ## 5 4 Sep 1981 ## 6 11/7/1987 ``` --- # breathe... -- ## there are really only 3 formats `dmy()` `ymd()` `mdy()` And `parse_date_time()` is your friend... --- ```r diff_dates <- diff_dates %>% mutate(free_date_parsed = parse_date_time(free_date, c("ymd", "mdy", "dmy"))) head(diff_dates, 8) ``` ``` ## # A tibble: 8 × 2 ## free_date free_date_parsed ## <chr> <dttm> ## 1 30 June 1978 1978-06-30 00:00:00 ## 2 17 Feb, 2011 2011-02-17 00:00:00 ## 3 March 25 2014 2014-03-25 00:00:00 ## 4 1977/7/11 1977-07-11 00:00:00 ## 5 4 Sep 1981 1981-09-04 00:00:00 ## 6 11/7/1987 1987-07-11 00:00:00 ## 7 sep 11 77 1977-09-11 00:00:00 ## 8 6th july 67 2067-07-06 00:00:00 ``` -- - `parse_date_time()` failed to parse when there was no year. - it made an error when the year was only 2 digits. --- # Take home message Lubridate is full of functions that make working with dates in R easier - Use date components in ggplot with... + `day()`, `wday()`, `month()`, `year()` -- - Convert characters to dates with... + `dmy()`, `mdy()`, `ymd()` -- - Whether R will recognise your dates depends on + what format they are in + AND where you are reading them from -- BUT `parse_date_time()` does a pretty good job 😄 --- class: inverse, center, middle # how you can help others learn new things -- ### come to our Show and Tell events (next Nov 17) -- ### offer to teach an R-Ladies Sydney workshop -- ### and blog while you are learning --- # Read about my learning process My blog = [www.jenrichmond.rbind.io](http://www.jenrichmond.rbind.io/) 1. [first google](http://jenrichmond.rbind.io/post/lubridate-month-wday/) 2. [`wday()` `month()`](http://jenrichmond.rbind.io/post/lubridate-month-wday/) 3. [converting characters](http://jenrichmond.rbind.io/post/converting-characters-to-dates/) 4. [parsing dates automatically](http://jenrichmond.rbind.io/post/parsing-dates-automatically/) --- # thank you ! <iframe src="https://giphy.com/embed/xIJLgO6rizUJi" width="480" height="367" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/alice-in-wonderland-thank-you-xIJLgO6rizUJi">via GIPHY</a></p> ---