Rounding datetimes

Working with Dates and Times in R

Charlotte Wickham

Instructor

Rounding versus extracting

release_time <- releases$datetime
head(release_time)
"1997-12-04 08:47:58 UTC" "1997-12-21 13:09:22 UTC"
"1998-01-10 00:31:55 UTC" "1998-03-14 19:25:55 UTC"
"1998-05-02 07:58:17 UTC" "1998-06-14 12:56:20 UTC"
head(release_time) %>% hour()
8 13  0 19  7 12
head(release_time) %>% floor_date(unit = "hour")
"1997-12-04 08:00:00 UTC" "1997-12-21 13:00:00 UTC"
"1998-01-10 00:00:00 UTC" "1998-03-14 19:00:00 UTC"
"1998-05-02 07:00:00 UTC" "1998-06-14 12:00:00 UTC"
Working with Dates and Times in R

Rounding in lubridate

  • round_date() - round to nearest
  • ceiling_date() - round up
  • floor_date() - round to down
  • Possible values of unit:
    • "second", "minute", "hour", "day", "week", "month", "bimonth", "quarter", "halfyear", or "year".
    • Or multiples, e.g "2 years", "5 minutes"
Working with Dates and Times in R

Let's practice!

Working with Dates and Times in R

Preparing Video For Download...