bcongelio / nfl-analytics-with-r-book

The repo for Introduction to NFL Analytics with R (published with CRC Press)
https://bradcongelio.com/nfl-analytics-with-r-book/
Creative Commons Zero v1.0 Universal
51 stars 15 forks source link

Typo in 1.3.1 code example #7

Closed BenjaminBoo closed 11 months ago

BenjaminBoo commented 11 months ago

Hi, loving this book so far! I ran into a quick issue in section 1.3.1. The code offered is: oline_snap_counts <- oline_snap_counts %>% group_by(game_id, team) %>% arrange(player, .by_group = TRUE)

but line 3 should use arrange(player, .group_by = TRUE)

basically the words ".group_by" are just reversed. I was able to figure it out real quick with chat gpt, but in case people want to copy and past without fuss, I thought it would be worth highlighting.

Thanks!

bcongelio commented 11 months ago

The use of .by_group rather than .group_by is not a typo. It was done by choice. To be clear, both return the same information, but in a differing arrangement.

For example, using .by_group returns the following:

 game_id          week player             position team  offense_pct
   <chr>           <int> <chr>              <chr>    <chr>       <dbl>
 1 2022_01_BAL_NYJ     1 Ben Powers         G        BAL          1   
 2 2022_01_BAL_NYJ     1 Kevin Zeitler      G        BAL          1   
 3 2022_01_BAL_NYJ     1 Morgan Moses       T        BAL          1   
 4 2022_01_BAL_NYJ     1 Patrick Mekari     G        BAL          0.57
 5 2022_01_BAL_NYJ     1 Tyler Linderbaum   C        BAL          1   
 6 2022_01_BAL_NYJ     1 Alijah Vera-Tucker G        NYJ          1   
 7 2022_01_BAL_NYJ     1 Connor McGovern    C        NYJ          1   
 8 2022_01_BAL_NYJ     1 George Fant        T        NYJ          1   
 9 2022_01_BAL_NYJ     1 Laken Tomlinson    G        NYJ          1   
10 2022_01_BAL_NYJ     1 Max Mitchell       T        NYJ          1 

While using .group_by returns the following:

 game_id          week player    position team  offense_pct
   <chr>           <int> <chr>     <chr>    <chr>       <dbl>
 1 2022_01_IND_HOU     1 A.J. Cann G        HOU          1   
 2 2022_02_HOU_DEN     2 A.J. Cann G        HOU          1   
 3 2022_03_HOU_CHI     3 A.J. Cann G        HOU          1   
 4 2022_04_LAC_HOU     4 A.J. Cann G        HOU          1   
 5 2022_05_HOU_JAX     5 A.J. Cann G        HOU          1   
 6 2022_07_HOU_LV      7 A.J. Cann G        HOU          1   
 7 2022_09_PHI_HOU     9 A.J. Cann G        HOU          1   
 8 2022_10_HOU_NYG    10 A.J. Cann G        HOU          0.94
 9 2022_11_WAS_HOU    11 A.J. Cann G        HOU          1   
10 2022_12_HOU_MIA    12 A.J. Cann G        HOU          1  

Because I am interested in seeing the specific combinations of offensive linemen used by a team, per week, I opted to use .by_group to see this, rather than group_by which returns the information on a per-player basis.