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
