+ - 0:00:00
Notes for current slide
Notes for next slide

Date Night

using lubridate to work with dates in R

Jen Richmond

2021-12-10

1 / 41

Useful links

Code along...https://rstudio.cloud/project/1817769

Slides...https://jennyslides.netlify.app/lubridate/

🐦 @allisonhorst

2 / 41

The Plan

3 / 41

The Plan

an opinionated guide to learning new things in R

3 / 41

The Plan

an opinionated guide to learning new things in R

3 things I learned about lubridate

3 / 41

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

3 / 41

an opinionated guide to learning new things in R

4 / 41

learning new things in R

The best thing about open source software like R is that there are SO many learning resources.

5 / 41

learning new things in R

The best worst thing about open source software like R is that there are SO many learning resources.

6 / 41

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...

via GIPHY

6 / 41

image credit: Daniela Procida PyCon

7 / 41

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.

image credit: Daniela Procida PyCon

8 / 41

image credit: Daniela Procida PyCon

9 / 41

image credit: Daniela Procida PyCon

10 / 41

image credit: Daniela Procida PyCon

11 / 41

in the context of R...

reference

  • package documentation
    • CRAN docs
    • package vignette
    • cheat sheets
13 / 41

my opinion

skip tutorials and go straight to "how to" guides

image credit: https://documentation.divio.com/introduction/

14 / 41

why?

tutorials lack an interesting context/problem

15 / 41

without context/problem...

via GIPHY

16 / 41
17 / 41

how I learned lubridate

first google...

18 / 41

.... I hit GOLD

Julia Silge blog

  • great data set (London theatre from 1600)
  • solves common date problems
    • make characters into dates
    • plot by year(), month(), wday()
    • work with time spans

🐦 @juliasilge

19 / 41

My learning plan

Find a cool dataset and copy Julia.

dataset = COVID testing rates NSW

SMH: Sept 15 2020

20 / 41

3 things I learned about lubridate

21 / 41

3 things I learned about lubridate

1. plotting with date components

21 / 41

3 things I learned about lubridate

1. plotting with date components

2. converting characters to dates

21 / 41

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

21 / 41

1. plotting with date components

22 / 41

date plots

Use date components within group_by() + summarise() or within ggplot() using...

year()

month()

day()

wday()

23 / 41

2. converting characters to dates

25 / 41

So R thinks your dates are characters...

If your dates are in a consistent format, you can use...

ymd()

mdy()

dmy()

... to convert characters to dates

26 / 41

3. how to make R read your dates as dates

28 / 41

if only it were consistent...

Why did the covid testing data parse as dates...

29 / 41

if only it were consistent...

Why did the covid testing data parse as dates...

... but the Taylor/Beyonce data did not?

29 / 41
sales <- read_csv(here::here("data", "sales.csv"))
sales$released[[1]]
## [1] "October 24, 2006"
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

30 / 41

collecting data to test my hypothesis

google form: enter your birthday 3 times

  • as you like
  • using a calendar widget
  • in year-month-day format (ISO 8601)

31 / 41

Which dates does R think are dates?

it depends....

32 / 41

using googlesheets4 package

  • free date = list
  • template date = date 😄
  • iso date = char

from .xlsx

  • free date = char
  • template date = date 😄
  • iso date = char
33 / 41

using googlesheets4 package

  • free date = list
  • template date = date 😄
  • iso date = char

from .xlsx

  • free date = char
  • template date = date 😄
  • iso date = char

from .csv

  • free date = char
  • template date = char
  • iso date = date 😠
33 / 41

what to do with dates in a million formats?

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
34 / 41

breathe...

35 / 41

breathe...

there are really only 3 formats

dmy()

ymd()

mdy()

And parse_date_time() is your friend...

35 / 41
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
36 / 41
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.

36 / 41

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()
37 / 41

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()
37 / 41

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
37 / 41

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 😄

37 / 41

how you can help others learn new things

38 / 41

how you can help others learn new things

come to our Show and Tell events (next Nov 17)

38 / 41

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

38 / 41

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

38 / 41

thank you !

via GIPHY

40 / 41
41 / 41

Useful links

Code along...https://rstudio.cloud/project/1817769

Slides...https://jennyslides.netlify.app/lubridate/

🐦 @allisonhorst

2 / 41
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow