这是一个简短的 data.table:

DT <- data.table(Tag1 = c(22,253,6219,6219,252862,252864,312786,312812),
                 Tag2 = c(22,255,6220,252857,252863,252865,251191,252863),
                 Date= as.Date(as.character(c("7/25/2008","6/15/2000","6/30/2000","9/6/2002","9/6/2002","9/6/2002","9/3/2003","9/5/2003")),format = "%m/%d/%Y"))
DT

     Tag1   Tag2       Date
1:     22     22 2008-07-25
2:    253    255 2000-06-15
3:   6219   6220 2000-06-30
4:   6219 252857 2002-09-06
5: 252862 252863 2002-09-06
6: 252864 252865 2002-09-06
7: 312786 251191 2003-09-03
8: 312812 252863 2003-09-05

我想按 3 列升序对 data.table 进行排序:Tag1、Tag2 和 Date。
我测试过:
> test <- DT[order(Tag1, Tag2, Date)]
> test
     Tag1   Tag2       Date
1:     22     22 2008-07-25
2:    253    255 2000-06-15
3:   6219   6220 2000-06-30
4:   6219 252857 2002-09-06
5: 252862 252863 2002-09-06
6: 252864 252865 2002-09-06
7: 312786 251191 2003-09-03
8: 312812 252863 2003-09-05

但是,我想按如下方式对 data.table 进行排序:
> test
         Tag1   Tag2       Date
    1:     22     22 2008-07-25
    2:    253    255 2000-06-15
    3:   6219   6220 2000-06-30
    4:   6219 252857 2002-09-06
    5: 252862 252863 2002-09-06
    6: 312812 252863 2003-09-05
    7: 252864 252865 2002-09-06
    8: 312786 251191 2003-09-03

特别是,Tag1 或 Tag1 的重复值应该一个在另一个之下(例如:Tag1 为 6219,Tag2 为 252863)。
我怎样才能做到这一点 ?

编辑 :

建议的解决方案适用于简短的 data.table(如上面的 data.table)。
这是一个更长的版本:
DT <- data.table(Tag1 = c(252860, 252862, 312812, 252864, 252866, 252868, 252870, 318880, 252872, 252874, 252876, 252878, 252880, 252880, 252881, 252883,
252885, 252887, 311264, 252889, 252889, 252892, 318879, 318880, 318881), Tag2 = c(252861, 252863, 252863, 252865, 252867, 252869, 252871, 252871, 252873,
252875, 252877, 252879, 414611, 905593, 252882, 252884, 252886, 252888, 252888, 252890, 318904, 252893, 318878, 414547, 318882), Date = c("9/6/2002",
"9/6/2002", "9/5/2003", "9/6/2002", "9/6/2002", "9/6/2002", "9/6/2002", "10/8/2003", "9/6/2002", "9/6/2002", "9/6/2002", "9/6/2002", "10/5/2004",
"9/6/2002", "9/6/2002", "9/6/2002", "9/10/2002", "9/10/2002", "7/15/2003", "9/10/2002", "10/15/2003", "9/10/2002", "10/8/2003", "9/29/2004","10/8/2003"))

这是预期的结果(即 data.table "After")。特别是,data.table "After"应该遵守两个条件:

1) 行按日期升序排列

2) Tag1 或 Tag1 的重复值排在另一个下方 (最终不需要按升序排列)

Tag1 和 Tag2 的所有重复值都显示为黄色。

r - 按特定值顺序对数据表进行排序-LMLPHP

最佳答案

旧订单

df[order(Tag1, Tag2, Date)]
#      Tag1   Tag2       Date
# 1:     22     22 2008-07-25
# 2:    253    255 2000-06-15
# 3:   6219   6220 2000-06-30
# 4:   6219 252857 2002-09-06
# 5: 252862 252863 2002-09-06
# 6: 252864 252865 2002-09-06
# 7: 312786 251191 2003-09-03
# 8: 312812 252863 2003-09-05

新订单
按降序对 Date 列进行排序,然后按升序对 Tag1 列进行排序,并按 Tag2 分组。
setcolorder(dt1 <- df[order(-Date)][order(Tag1), .SD, by = Tag2], colnames(df))

dt1
#      Tag1   Tag2       Date
# 1:     22     22 2008-07-25
# 2:    253    255 2000-06-15
# 3:   6219 252857 2002-09-06
# 4:   6219   6220 2000-06-30
# 5: 252862 252863 2002-09-06
# 6: 312812 252863 2003-09-05
# 7: 252864 252865 2002-09-06
# 8: 312786 251191 2003-09-03

评论中@akrun 的解决方案扰乱了数据的结构。这是比较。看 #4:6219 应该有 252857 而不是 251191
df[,lapply(df, sort)]
#      Tag1   Tag2       Date
# 1:     22     22 2000-06-15
# 2:    253    255 2000-06-30
# 3:   6219   6220 2002-09-06
# 4:   6219 251191 2002-09-06
# 5: 252862 252857 2002-09-06
# 6: 252864 252863 2003-09-03
# 7: 312786 252863 2003-09-05
# 8: 312812 252865 2008-07-25

关于r - 按特定值顺序对数据表进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38879961/

10-12 17:19