A frequency table counts observations for each category or combination of categorical values. In R, janitor::tabyl() can cross-tabulate event_type for every play_length2 value, producing a data frame ready for further analysis. section 8.5.1
A practical workflow is:
- Filter the data to the relevant team, possession-time range, and event types. section 8.5.1
- Call tabyl(play_length2, event_type) to count shots and misses at each second of possession time. section 8.5.1
- Select the shot and miss counts, then derive field-goal percentage as shot divided by shot plus miss. section 8.5.1
Milwaukee Bucks shooting by possession time
MIL %>% tabyl(play_length2, event_type) -> MILy creates counts for each possession-second value and event type. Selecting play_length2, shot, and miss, then calculating fg_pct, shows 206 shots and 219 misses at five seconds, for a 48.47% field-goal percentage. The resulting table can support a line chart alongside average points scored. 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 complete frequency-table output, including shot and miss counts and the derived field-goal percentages for each possession-second value. section 8.5.1
