liveBook · Manning

Date class

As explained in Statistics Slam Dunk

A date class is an R object representation for calendar dates that supports date-specific storage and operations.

A date class is an R object representation for calendar dates. Converting a character column to this class makes the column explicitly date-based instead of leaving values as strings. section 16.2

A practical date-class workflow is:

  • Select the date column alongside the variables needed for analysis. section 16.2
  • Convert an ISO-formatted value such as 2019-10-22 with as.Date(date, "%Y-%m-%d"). section 16.2
  • For dates formatted as 10/27/18 or 10/27/2018, supply the matching format string, such as "%m/%d/%Y". section 17.3
  • Derive calendar labels such as month with format(date, "%B"), then create ordered month values when analysis needs season order. section 17.3

NBA free-throw dates

After filtering play-by-play data to free throws, convert its date column and verify the result: ft$date <- as.Date(ft$date, "%Y-%m-%d"); class(ft$date) returns "Date". The resulting data retains dates such as 2019-10-22 while other columns remain factors, character strings, or integers. section 16.2

ft$date <- as.Date(ft$date, "%Y-%m-%d")
class(ft$date)
## [1] "Date"

Inspect the executable conversion from an ISO-formatted character column to Date and verify its class. section 16.2

oddsdf2$date <- as.Date(oddsdf2$date, "%m/%d/%Y")

Compare the conversion syntax for slash-formatted dates, then use the related month extraction listing to create calendar features. section 17.3

How Statistics Slam Dunk covers it

How deep it goes
Demonstrates format-specific conversion, class verification, month extraction, and ordered month coding without developing broader date operations.
Examples
Uses 2019–20 NBA play-by-play dates and 2018–19 NBA schedule dates to create Date and month variables.
What it assumes
Assumes character columns and basic data wrangling, while demonstrating date conversion and calendar-variable creation directly.
What you can do after
Apply it via base R as.Date() and verify the resulting Date class before deriving month features.
How it connects
Connects character strings with as.Date(), dplyr mutate(), format(), case_when(), and factor variables.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.