Arrange within a group with dplyr

后端 未结 2 800
无人共我
无人共我 2021-01-17 16:44

I am using the library(nycflights13) and I use the following command to group_by month and day, select the top 3 rows within each group and then sort in descending order wit

相关标签:
2条回答
  • 2021-01-17 17:11

    I think the problem in your second example is that your are using desc on all the variables at the same time, so it is only applied to the month column.

       flights %>% group_by(month, day)  %>% top_n(3, dep_delay) %>% 
            arrange(
                month, 
                day,  
                desc(dep_delay)
            )
    
    Source: local data frame [1,108 x 19]
    Groups: month, day [365]
    
        year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin
       <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>     <dbl>   <chr>  <int>   <chr>  <chr>
    1   2013     1     1      848           1835       853     1001           1950       851      MQ   3944  N942MQ    JFK
    2   2013     1     1     2343           1724       379      314           1938       456      EV   4321  N21197    EWR
    3   2013     1     1     1815           1325       290     2120           1542       338      EV   4417  N17185    EWR
    4   2013     1     2     2131           1512       379     2340           1741       359      UA    488  N593UA    LGA
    5   2013     1     2     1607           1030       337     2003           1355       368      AA    179  N324AA    JFK
    6   2013     1     2     1412            838       334     1710           1147       323      UA    468  N474UA    EWR
    7   2013     1     3     2056           1605       291     2239           1754       285      9E   3459  N928XJ    JFK
    8   2013     1     3     2008           1540       268     2339           1909       270      DL   2027  N338NW    JFK
    9   2013     1     3     2012           1600       252     2314           1857       257      B6    369  N558JB    LGA
    10  2013     1     4     2123           1635       288     2332           1856       276      EV   3805  N29917    EWR
    # ... with 1,098 more rows, and 6 more variables: dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
    #   time_hour <dttm>
    
    0 讨论(0)
  • 2021-01-17 17:19

    You need to add .by_group=T to arrange within groups.

    flights %>%
       group_by(month, day) %>%
       top_n(3, dep_delay) %>%
       arrange(dep_delay, .by_group = TRUE)
    
    
    0 讨论(0)
提交回复
热议问题