liveBook · Manning

As.numeric() function

As explained in Statistics Slam Dunk

The as.numeric() function in R converts a compatible object into numeric values.

`as.numeric()` converts compatible R objects to numeric values. It is useful when a column looks usable but is stored as logical values or character strings, and later counting, arithmetic, or visualization needs numbers. section 6.4.6 section 8.4

Turn game-result flags into counts

A result table stores six outcome columns as logical values. Applying `as.numeric()` changes `FALSE` to `0` and `TRUE` to `1`, so the rows containing `1` can be replaced with their corresponding game counts before reshaping the table. section 6.4.6

tbl21$HHH <- as.numeric(tbl21$HHH)
tbl21$HRH <- as.numeric(tbl21$HRH)
tbl21$RHH <- as.numeric(tbl21$RHH)
tbl21$RRR <- as.numeric(tbl21$RRR)
tbl21$RHR <- as.numeric(tbl21$RHR)
tbl21$HRR <- as.numeric(tbl21$HRR)

This listing converts each logical outcome column individually, preparing the table for indexed count replacement and `pivot_longer()`. section 6.4.6

Extract seconds from play lengths

After extracting the final two characters of values such as `00:00:12`, convert the new character column with `as.numeric()`; the resulting values include `12`, `18`, and `0` seconds. section 8.4

The same step can prepare formatted points-per-game strings for later numeric standardization, or coerce probability and median vectors before multiplying them to calculate expected value. section 19.3.2 section 3.8

How Statistics Slam Dunk covers it

How deep it goes
Demonstrates direct column and vector conversions with observed outputs, without detailing coercion rules for incompatible inputs.
Examples
Uses NBA game-result flags, play-length strings, points-per-game values, and draft-pick probability and median vectors.
What it assumes
Assumes basic R data columns and assignment while illustrating conversion after string extraction or logical grouping.
What you can do after
Apply it to convert prepared columns or vectors before numerical analysis and calculation.
How it connects
Connects to logical values, character strings, `str_sub()`, `pivot_longer()`, standardization, and expected value.

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.