0

I am trying to sort a data matrix with several columns. Here, I want to sort sequentially as in Excel. Once I sort the matrix by the first column, I would like to keep the first column, and then sort by the second column, then keep the first and second columns and then sort the rest of the matrix by the third column, and so on.

For example, if my matrix is dd and I want to sort from 20 to 34 sequentially:

L <- 34
for(init in 20:L){
    dd <- dd[with(dd, order(dd[,init],decreasing=T)), ]
                }

This does not work; can anyone advise me with a correct script for this case?

Ben Bolker
  • 173,430
  • 21
  • 312
  • 389
chuck
  • 11
  • 2
  • possible duplicate of [How to sort a dataframe by column(s) in R](http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r) – Dirk Eddelbuettel Jul 06 '11 at 13:43
  • do you mean that you want to order by the 20th through 34th columns of your data frame? – Ben Bolker Jul 06 '11 at 13:43
  • @Dirk Eddelbuettel: I think not quite because the OP wants to sort (if I am interpreting correctly) by a lot of columns, and it would be a bit tedious to do them all by hand. – Ben Bolker Jul 06 '11 at 13:45
  • yes,ben's comment is right- i am trying to do it as excel - sory by 1st column, using the sorted matrix, then by 2nd column, using the sorted matrix, then by 3rd column, in excel, there is the option to do it with sort by + then by + then by, i would like to do it by 20 to 34 columns for a matrix with 64 columns) – chuck Jul 06 '11 at 17:22
  • It would be a bit clearer if you had some data in there - for example, I guess your "matrix" is actually a data.frame... The answers below don't work for matrices... – Tommy Jul 06 '11 at 18:48

1 Answers1

2

Perhaps

ord <- do.call(order,c(dd[,20:34],decreasing=TRUE))
dd <- dd[ord,]

edit: @DWin points out that as.list is not necessary (and that the help page for ?order has a very similar example)

edit 2: if you want the sort to be decreasing, and you need to use do.call to specify many columns, you need to include decreasing=TRUE in the argument list as above by using c() to lump it in with the data frame. The following simplified example appears to work:

X <- rev(expand.grid(x=1:2,y=1:3,z=1:4))
> head(X)
  z y x
1 1 1 1
2 1 1 2
3 1 2 1
4 1 2 2
5 1 3 1
6 1 3 2
ord <- do.call(order,c(X,decreasing=TRUE))
head(X[ord,])
head(X[ord,])
   z y x
24 4 3 2
23 4 3 1
22 4 2 2
21 4 2 1
20 4 1 2
19 4 1 1
Ben Bolker
  • 173,430
  • 21
  • 312
  • 389
  • 1
    I tested it ... you do not need the `as.list`. _And_ there is a worked example on the help page for `order` under the heading "Sorting data frames" – IRTFM Jul 06 '11 at 14:10
  • when i sort with decreasing=T, do.call(order(decreasing=T),dd[,20:34]) does not work. how to modify with decreasing option in do.call? – chuck Jul 06 '11 at 17:36
  • whenever being sorted, it should be decreasing. – chuck Jul 06 '11 at 17:39
  • do.call(order,c(dd[,20:34],decreasing=T)) i tried with this, but it seems it is sorted by 20th column with decreasing option but from 21th, it is increasing. it seems the option works for the 1st sorting. – chuck Jul 06 '11 at 17:41
  • Hmmm. A reproducible example would be *extremely* useful. – Ben Bolker Jul 06 '11 at 17:43
  • i wanted to try to use sortData function in Deducer library---but substantially required library for Deducer library---iplots library has a problem---i got the same error---so could not use Deducer library + sortDat function on mac ---See this link for error---http://www.readmespot.com/question/o/3985829/problem-with-iplots-library-which-prevents-jgr-to-start-on-macos – chuck Jul 06 '11 at 17:56
  • ?? I'm sure the base R functions can solve the problem you posed above, I would prefer not to get into additional packages/complexities if at all avoidable ... does the edited version above solve your problem? If not, please explain clearly why/provide a counterexample ... – Ben Bolker Jul 06 '11 at 17:59
  • sorry---doBy library works. got the matrix i wanted. thanks a lot. – chuck Jul 06 '11 at 18:07
  • ?? can you explain why the solution I provided above *doesn't* work?? or provide your example with `doBy` as an additional answer to your question below (you're allowed to answer your own question)? – Ben Bolker Jul 06 '11 at 18:13
  • well i did NEVER say the solution you provided did not work. i was trying to do a couple of ways to resolve it and when i used sortData function got the error message, so just added the comment. that is it. i do not really understand why you are so sensitively responding in this forum now. – chuck Jul 06 '11 at 18:28
  • 2
    Because it's frustrating to put time into solving someone's problem (and figuring out the precise meaning of their question in the first place) and having them say that (1) your solution doesn't work for them [without showing a reproducible example] and (2) they found a different solution, without specifying what they did (it would be helpful if you could document your `doBy` solution, for my benefit and for the benefit of future users who are reading this question) ... – Ben Bolker Jul 06 '11 at 18:55
  • 1
    @chuck - the frustration can often arise when someone who is trying to provide assistance has to go through mental gymnastics to figure out what it is you're talking about. Providing a clear, reproducible example is the number one way to get quick and efficient help. Additionally, if you've identified a better, alternative solution - by all means post it! People six, twelve, eighteen months from now who find this post may be interested in both solutions. – Chase Jul 06 '11 at 19:43
  • @chuck -- you say above that `do.call(order,c(dd[,20:34],decreasing=T))` doesn't work. That seems pretty similar to what I suggested above (unless you happened to have redefined the variable `T`, or unless you tried that command on its own and not in a context similar to what I suggested above), and so I gathered that my suggestion did *not* work for you. Could you try my latest (edited) solution and see if it does work, and if not try to give enough information for further troubleshooting? – Ben Bolker Jul 06 '11 at 22:43