class: center, middle, inverse, title-slide .title[ # Stat 585 - Working with Date and Time ] .author[ ### Heike Hofmann ] --- ## `lubridate` package - package for working with dates and times - defines different classes of time: instants, periods, intervals, durations - defines converter and accessor functions, enables time calculus Resources: Cheatsheet for [lubridate](https://rawgit.com/rstudio/cheatsheets/main/lubridate.pdf) package Chapter [Dates and Times](https://r4ds.had.co.nz/dates-and-times.html) in R for Data Science --- ## Converter functions and instants in time - time instants: one (absolute) moment in time, e.g. `now()`, `Jan-1-2000` - easy-to-use converter functions: - date: `ymd`, `mdy`, `dmy`, ... - time: `hm`, `hms`, ... - date & time: `ymd_hms`, `mdy_hm`, ... - order of letters determines how strings are parsed - separators are automatically determined, then assumed to be the same Example: ```r mdy("04-18-2022") ``` ``` ## [1] "2022-04-18" ``` --- ## Accessor functions - accessor functions: `year`, `month`, `week`, `wday`, `mday`, `yday`, `hour`, `minute`, ... - accessor functions can also be used for setting elements of date and time, e.g. `hour(x) <- 12` Examples: ```r month(now()) ``` ``` ## [1] 2 ``` ```r wday(now(), label = TRUE) ``` ``` ## [1] Thu ## Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat ``` --- ## Intervals and Durations - Intervals have a *start* and an *end* date/time: absolute difference - Durations are potentially of relative length (months, leap year, leap second, ...) ```r end_date <- now() # span is interval, years(1) is duration span <- end_date - years(1) span ``` ``` ## [1] "2022-02-09 00:07:10 CST" ``` ```r end_date - days(10) ``` ``` ## [1] "2023-01-30 00:07:10 CST" ``` --- class: inverse ## Your Turn How many days is it until your birthday? What day of the week will it be this year? On which day of the week were you born? How often is the 13th of a month a Friday? Based on the last ten years, are there some days that are more likely to be on a Friday than others? --- ## Your Turn Solutions ```r birthday <- ymd("2022-05-01") # (this is my daughter's birthday) birthday - today() ``` ``` ## Time difference of -284 days ``` ```r wday(birthday, label = TRUE) ``` ``` ## [1] Sun ## Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat ``` ```r wday(ymd("2014-05-01"), label = TRUE) ``` ``` ## [1] Thu ## Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat ``` --- ```r dates <- seq.Date(from=ymd("2010-01-01"), to=ymd("2021-12-31"), by = 1) thirteens <- dates[mday(dates) == 13] table(wday(thirteens, label=TRUE)) ``` ``` ## ## Sun Mon Tue Wed Thu Fri Sat ## 20 21 20 22 19 21 21 ``` --- ```r dframe <- data.frame(dates = dates, weekdays = wday(dates, label=TRUE), month_days = mday(dates)) dframe %>% ggplot(aes(x = month_days, fill = weekdays)) + geom_bar() ``` ![](02_date-and-time_files/figure-html/unnamed-chunk-6-1.png)<!-- --> --- ```r xtabs(~weekdays+month_days, data = dframe) ``` ``` ## month_days ## weekdays 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ## Sun 21 19 22 20 21 20 21 21 19 22 20 21 20 21 21 19 22 20 21 20 21 21 19 ## Mon 21 21 19 22 20 21 20 21 21 19 22 20 21 20 21 21 19 22 20 21 20 21 21 ## Tue 20 21 21 19 22 20 21 20 21 21 19 22 20 21 20 21 21 19 22 20 21 20 21 ## Wed 21 20 21 21 19 22 20 21 20 21 21 19 22 20 21 20 21 21 19 22 20 21 20 ## Thu 20 21 20 21 21 19 22 20 21 20 21 21 19 22 20 21 20 21 21 19 22 20 21 ## Fri 22 20 21 20 21 21 19 22 20 21 20 21 21 19 22 20 21 20 21 21 19 22 20 ## Sat 19 22 20 21 20 21 21 19 22 20 21 20 21 21 19 22 20 21 20 21 21 19 22 ## month_days ## weekdays 24 25 26 27 28 29 30 31 ## Sun 22 20 21 20 21 20 17 13 ## Mon 19 22 20 21 20 19 20 11 ## Tue 21 19 22 20 21 19 18 13 ## Wed 21 21 19 22 20 20 19 11 ## Thu 20 21 21 19 22 19 19 12 ## Fri 21 20 21 21 19 20 19 12 ## Sat 20 21 20 21 21 18 20 12 ```