class: center, middle <style> .inverse { background-color: #141417; } </style> # Chopping dates with santoku 0.4.0 ## 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) apr_aug_1 <- as.Date(c("2000-04-01", "2000-08-01")) chopped <- chop(y2k_sample, apr_aug_1) ``` <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 # Nice labels are important. ```r # like table(chop(...)): tab(y2k_sample, apr_aug_1, labels = lbl_discrete(fmt = "%b")) ``` ``` ## Jan—Mar Apr—Jul Aug—Dec ## 20 20 20 ``` --- 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: 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)