這裏的數據表是一個短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和日期。 我測試:
> 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 「後」)。特別地,data.table「後」應該尊重兩個條件:
1)行由日期升序排序
2)Tag1中或Tag1中重複的值低於一個設置其他(最終不需要按升序排列)
Tag1和Tag2的所有重複值均爲黃色。
那麼你可能需要打破'order'。即'DT [,lapply(DT,sort)]' – akrun
@Sathish這是一個評論,而不是一個答案。這就是我沒有發佈的原因。 – akrun
您需要使用哪種數據來進行這種分組/排序? – HubertL