0

I have a similar problem with my own dataset and decided to practice on an example dataset. I'm trying to select the TailNumbers associated with the Max Air Time by Carrier.

Here's my solution thus far:

library(hflights)

hflights %>%
  group_by(UniqueCarrier, TailNum) %>%
  summarise(maxAT = max(AirTime)) %>%
  arrange(desc(maxAT))

This provides three columns to which I can eyeball the max Air Time values and then filter them down using filter() statements. However, I feel like there's a more elegant way to do so.

D500
  • 412
  • 4
  • 15

1 Answers1

1

You can use which.max to find out the row with the maximum AirTime and then slice the rows:

hflights %>% 
    select(UniqueCarrier, TailNum, AirTime) %>% 
    group_by(UniqueCarrier) %>% 
    slice(which.max(AirTime))

# A tibble: 15 x 3
# Groups:   UniqueCarrier [15]
#   UniqueCarrier TailNum AirTime
#           <chr>   <chr>   <int>
# 1            AA  N3FNAA     161
# 2            AS  N626AS     315
# 3            B6  N283JB     258
# 4            CO  N77066     549
# 5            DL  N358NB     188
# 6            EV  N716EV     173
# 7            F9  N905FR     190
# 8            FL  N176AT     186
# 9            MQ  N526MQ     220
#10            OO  N744SK     225
#11            UA  N457UA     276
#12            US  N950UW     212
#13            WN  N256WN     288
#14            XE  N11199     204
#15            YV  N907FJ     150
Psidom
  • 171,477
  • 20
  • 249
  • 286
  • Awesome. So slice takes the max values that correspond to all the columns in the selected data? I'll play with it and figure it out. – D500 Nov 16 '17 at 22:51