Why lubridate?

Working with Dates and Times in R

Charlotte Wickham

Instructor

lubridate

  • Make working with dates and times in R easy!
  • tidyverse package
    • Plays nicely with builtin datetime objects
    • Designed for humans not computers
  • Plays nicely with other tidyverse packages
  • Consistent behaviour regardless of underlying object
Working with Dates and Times in R

Parsing a wide range of formats

ymd("2013-02-27")
"2013-02-27"
dmy("27/2/13")
"2013-02-27"
parse_date_time(c("Feb 27th, 2017", "27th Feb 2017"), 
+   order = c("mdy", "dmy"))
"2017-02-27 UTC" "2017-02-27 UTC"
Working with Dates and Times in R

Manipulating datetimes

# Extract components
akl_daily <- akl_daily %>%
  mutate(
    year = year(date),
    yday = yday(date),
    month = month(date, label = TRUE)
  )

Working with Dates and Times in R

Time spans

# A tibble: 131 x 3
            name              period 
           <chr>        <S4: Period> 
1  Elizabeth II 65y 7m 27d 0H 0M 0S 
2      Victoria  63y 7m 2d 0H 0M 0S 
3      George V 25y 8m 14d 0H 0M 0S 
4    George III 19y 0m 28d 0H 0M 0S 
5     George VI 15y 1m 26d 0H 0M 0S 
Working with Dates and Times in R

Other lubridate features

  • Handling timezones
  • Fast parsing of standard formats
  • Outputting datetimes
Working with Dates and Times in R

Let's practice!

Working with Dates and Times in R

Preparing Video For Download...