[R] Sort a data table

Nelly Reduan nell.redu at hotmail.fr
Thu Aug 11 17:19:54 CEST 2016


Hello,

I have some problems to sort a large data table. In particular, I would like to sort a data table in such a way that the duplicated values in two columns are disposed one below the other and if possible in an ascending order (especially for the column "Date"). An example with a short data table and associated responses are showed in  http://stackoverflow.com/questions/38879961/sort-a-data-table-with-specific-value-order) . However, the proposed solutions don't work with this data table. For example, the line 24 with the value 318880 for Tag1 is not below the line 8.

[http://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a]<http://stackoverflow.com/questions/38879961/sort-a-data-table-with-specific-value-order>

Sort a data table with specific value order<http://stackoverflow.com/questions/38879961/sort-a-data-table-with-specific-value-order>
stackoverflow.com
I have a 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= a...



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 = as.Date(as.character(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")),format = "%m/%d/%Y"))

setcolorder(dt1 <- DT[order(-Date)][order(Tag1), .SD, by = Tag2], colnames(DT))

> dt1

      Tag1   Tag2       Date

 1: 252860 252861 2002-09-06

 2: 252862 252863 2002-09-06

 3: 312812 252863 2003-09-05

 4: 252864 252865 2002-09-06

 5: 252866 252867 2002-09-06

 6: 252868 252869 2002-09-06

 7: 252870 252871 2002-09-06

 8: 318880 252871 2003-10-08

 9: 252872 252873 2002-09-06

10: 252874 252875 2002-09-06

11: 252876 252877 2002-09-06

12: 252878 252879 2002-09-06

13: 252880 414611 2004-10-05

14: 252880 905593 2002-09-06

15: 252881 252882 2002-09-06

16: 252883 252884 2002-09-06

17: 252885 252886 2002-09-10

18: 252887 252888 2002-09-10

19: 311264 252888 2003-07-15

20: 252889 318904 2003-10-15

21: 252889 252890 2002-09-10

22: 252892 252893 2002-09-10

23: 318879 318878 2003-10-08

24: 318880 414547 2004-09-29

25: 318881 318882 2003-10-08


Thanks a lot for your time.

Have a nice day

Nell


	[[alternative HTML version deleted]]



More information about the R-help mailing list