class: center, middle <style> .inverse { background-color: #141417; } </style> # Chopping dates with santoku ## David Hugh-Jones <img src="figures/logo.png" style="width: 250px" /> --- class: middle, center # Santoku is a package for chopping data. ## It provides `chop()`, a replacement for `base::cut()`. --- class: middle, center # Santoku 0.4.0 can chop dates. <img src="figures/date.png" style="width: 300px;" /> --- class: inverse, middle ``` r y2k <- as.Date("2000-01-01") + 0:365 y2k_sample <- sample(y2k, 60) brk_dates <- as.Date(c("2000-01-15", "2000-04-01", "2000-08-01")) chopped <- chop(y2k_sample, brk_dates) ``` <img src="chopping-dates-with-santoku_files/figure-html/unnamed-chunk-2-1.png" width="90%" style="display: block; margin: auto;" /> --- class: inverse, top # You can use the usual `chop` functions. ``` r quartile <- chop_quantiles(y2k_sample, c(0.25, 0.75)) ``` <img src="chopping-dates-with-santoku_files/figure-html/unnamed-chunk-4-1.png" width="90%" style="display: block; margin: auto;" /> --- class: inverse # `lbl_date()` produces nice labels. ``` r x <- chop(y2k_sample, brk_dates, labels = lbl_date()) table(x) ``` ``` ## x ## 12-14 Jan 2000 15 Jan - 31 Mar 2000 1 Apr - 31 Jul 2000 ## 1 16 24 ## 1 Aug - 22 Dec 2000 ## 19 ``` --- class: inverse # `chop_width()` works. * It accepts `difftime`, `lubridate::Period` and `lubridate::Duration` widths. -- ``` r x <- chop_width(y2k, months(1), labels = lbl_endpoints(fmt = "%b")) table(x) ``` ``` ## x ## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ## 31 29 31 30 31 30 31 31 30 31 30 31 ``` -- * `Period` objects get the month right. --- class: center, middle # Chopping time works too. <img src="figures/thyme.jpg" /> --- background-image: url(figures/crew-dragon.jpg) background-size: cover class: inverse, middle <style> .hljs-tomorrow-night .hljs { opacity: 0.98; } </style> ``` r crew_dragon <- as.POSIXct(c( liftoff = "2020-05-30 15:22", dock = "2020-05-31 10:16"), tz = "America/New_York") uk_times <- seq(as.POSIXct("2020-05-30 18:00", tz = "GMT"), length.out = 24, by = "hours") stage <- chop(uk_times, crew_dragon, labels = c("pre-flight", "flight", "docked")) ``` <img src="chopping-dates-with-santoku_files/figure-html/unnamed-chunk-8-1.png" width="90%" style="display: block; margin: auto;" /> --- class: inverse # `lbl_datetime()` labels datetimes. ``` r x <- chop_width(uk_times, hours(4), labels = lbl_datetime(fmt = "%H:%M %b %d")) table(x) ``` ``` ## x ## 18:00-22:00 May 30 22:00 May 30 - 02:00 May 31 ## 4 4 ## 02:00-06:00 May 31 06:00-10:00 May 31 ## 4 4 ## 10:00-14:00 May 31 14:00-18:00 May 31 ## 4 4 ``` --- class: middle center # Happy chopping! <img src="figures/logo.png" style="width: 250px" /> [CRAN](https://cran.r-project.org/package=santoku) [github](https://hughjonesd.github.io/santoku)