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

Tibble not sorted in descending order of avg_cpoe #23

Closed BenjaminBoo closed 10 months ago

BenjaminBoo commented 10 months ago

2.5.4: NFL Data and the mutate() verb. I noticed that the following command includes an "arrange()" verb that should list the values in descending order of avg_cpoe: airyards_cpoe_mutate %>% filter(ay_distance == "Medium") %>% arrange(-avg_cpoe) %>% slice(1:10)

but in the tibble displayed in the book, and when I copy/paste into RStudio, the tibble is arranged in ascending alphabetical order of passer:

A tibble: 82 x 3 Groups: passer [82] passer ay_distance avg_cpoe

1 A.Brown Medium -6.09 2 A.Cooper Medium -43.1 3 A.Dalton Medium 5.01 4 A.Rodgers Medium 2.02 5 B.Allen Medium 44.5 6 B.Hoyer Medium -44.0 7 B.Mayfield Medium -15.4 8 B.Perkins Medium 0.266 9 B.Purdy Medium 9.72 10 B.Rypien Medium 18.0 i 72 more rows any idea why?
bcongelio commented 10 months ago

Good catch.

I forgot to ungroup() prior to using arrange() and that resulted in the error.

  filter(ay_distance == "Medium") %>%
  ungroup() %>%
  arrange(-avg_cpoe) %>%
  dplyr::slice(1:10)

Adding it, as in the above code, results in the correct output:

   passer    ay_distance avg_cpoe
   <chr>     <chr>          <dbl>
 1 T.Taylor  Medium          63.4
 2 J.Johnson Medium          51.2
 3 B.Allen   Medium          44.5
 4 R.Wright  Medium          40.4
 5 J.Driskel Medium          36.4
 6 N.Mullens Medium          31.7
 7 T.Hill    Medium          25.2
 8 S.Darnold Medium          19.2
 9 B.Rypien  Medium          18.0
10 J.Winston Medium          16.1