liveBook · Manning

Tabyl() function

As explained in Statistics Slam Dunk

The tabyl() function is a janitor package function that creates frequency tables from categorical variables in R data.

tabyl() from janitor creates a frequency table from categorical variables. With play_length2 and event_type, it counts shots and misses for each second of a Milwaukee Bucks possession. section 8.5.1

Count shots and misses by possession time

MIL %>% tabyl(play_length2, event_type) -> MILy MILy %>% select(play_length2, shot, miss) %>% mutate(fg_pct = shot / (shot + miss) * 100) -> MILy MILy$fg_pct <- round(MILy$fg_pct, digits = 2) The resulting table supplies shot and miss counts for each play_length2 value, which supports calculating field-goal percentage and plotting it by possession time. section 8.5.1

MIL %>%
 tabyl(play_length2, event_type) -> MILy
MILy %>%
 select(play_length2, shot, miss) %>%
 mutate(fg_pct = shot / (shot + miss)*100) -> MILy
MILy$fg_pct <- round(MILy$fg_pct, digits = 2)
print(MILy)
##  play_length2 shot miss fg_pct
##             5  206  219  48.47
##             6  193  218  46.96
##             7  173  187  48.06
##             8  176  183  49.03
##             9  161  169  48.79
##            10  155  200  43.66
##            11  171  205  45.48
##            12  165  210  44.00
##            13  167  191  46.65
##            14  178  188  48.63
##            15  164  167  49.55
##            16  125  158  44.17
##            17  126  133  48.65
##            18  113  130  46.50
##            19   62   91  40.52
##            20   73   77  48.67
##            21   43   63  40.57
##            22   34   52  39.53
##            23   30   37  44.78
##            24   19   24  44.19

Inspect the generated frequency table and its derived field-goal percentages. section 8.5.1

How Statistics Slam Dunk covers it

How deep it goes
Explains two-variable tabulation and follows the result through selection, percentage calculation, rounding, and plotting.
Examples
Uses Milwaukee Bucks play-by-play data to count shot outcomes at each second from 5 through 24 seconds of possession.
What it assumes
Assumes familiarity with R pipes, data frames, categorical columns, and dplyr functions such as select() and mutate().
What you can do after
Apply it via janitor to produce cross-tabulated counts for subsequent data transformation and analysis.
How it connects
Connects frequency tables with dplyr selection and mutation, field-goal percentage, and ggplot2 visualization.

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.