The base R as.Date() function converts suitable values, especially character strings, into Date-class calendar dates. Supply a format string when the input uses a recognizable pattern such as %Y-%m-%d, %Y/%m/%d, or %m/%d/%Y. Converting dates makes later wrangling, such as deriving calendar months, more reliable. section 16.2 section 17.3
Convert and verify an NBA date
For free-throw records stored in ISO format, convert the date column and confirm its class: ft$date <- as.Date(ft$date, "%Y-%m-%d") class(ft$date) ## [1] "Date" For betting data stored as month/day/year, use the matching format instead: oddsdf2$date <- as.Date(oddsdf2$date, "%m/%d/%Y") section 16.2 section 17.3
ft$date <- as.Date(ft$date, "%Y-%m-%d") class(ft$date) ## [1] "Date"
Inspect the executable conversion and class() check for the free-throw date variable. section 16.2
After conversion, format() can extract a month name for a new variable, while mutate() can use that value to create analysis-ready calendar categories. The format argument must match the input text; otherwise, the conversion may not represent the intended dates. section 17.3
