liveBook · Manning

As.Date() function

As explained in Statistics Slam Dunk

The as.Date() function in R converts suitable values, especially character strings, into Date-class calendar dates.

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

How Statistics Slam Dunk covers it

How deep it goes
Defines conversion, demonstrates format strings, verifies Date class, and derives months without explaining date internals.
Examples
Uses free-throw dates in %Y-%m-%d format and NBA betting dates in %m/%d/%Y format.
What it assumes
Assumes familiarity with data-frame columns and character strings while demonstrating date formats from the beginning.
What you can do after
Apply it via base R to convert character date columns and verify their Date class.
How it connects
Connects character strings with Date class, class(), format(), mutate(), and factor conversion.

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.