order() in data.table i

In  data.table ,  the order() in  DT[i]  can rearrange the order of the whole table with specific conditions, which works exactly the same with  arrange() in  dplyr .

 

flights[carrier == "AA", .(.N), by = .(origin, dest)][order(origin, -dest)]

   origin   dest     N
    <char> <char> <int>
 1:    EWR    PHX   121
 2:    EWR    MIA   848
 3:    EWR    LAX    62
 4:    EWR    DFW  1618
 5:    JFK    STT   229
 6:    JFK    SJU   690
 7:    JFK    SFO  1312
 8:    JFK    SEA   298
 9:    JFK    SAN   299
10:    JFK    ORD   432
11:    JFK    MIA  1876
12:    JFK    MCO   597
13:    JFK    LAX  3387
14:    JFK    LAS   595
15:    JFK    IAH     7
16:    JFK    EGE    85
17:    JFK    DFW   474
18:    JFK    DCA   172
19:    JFK    BOS  1173
20:    JFK    AUS   297
21:    LGA    PBI   245
22:    LGA    ORD  4366
23:    LGA    MIA  3334
24:    LGA    DFW  3785
    origin   dest     N

  

 

flights %>% arrange(origin)

flights %>% arrange(desc(origin))

  

posted @ 2023-06-27 22:01  DaqianLU  阅读(27)  评论(0)    收藏  举报