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
