[R] Ordering every row of a matrix while ignoring off diagonal elements

From: Vivian Shih <vivs_at_ucla.edu>
Date: Sun, 03 Apr 2011 20:51:33 -0700


Sorry if this is a stupid question but I've been stuck on how to code so that I can order rows of a matrix without paying attention to the diagonal elements.

Say, for example, I have a matrix d:
> d

          [,1] [,2] [,3] [,4]
[1,] 0.000000 2.384158 2.0065682 2.2998856
[2,] 2.384158 0.000000 1.4599928 2.4333213
[3,] 2.006568 1.459993 0.0000000 0.9733285
[4,] 2.299886 0.000000 0.9733285 0.0000000

Then I'd like ordered d to be like:

      [,1] [,2] [,3]
[1,] 3 4 2
[2,] 3 1 4
[3,] 4 2 1
[4,] 2 3 1

So subject 1's smallest value is in column 3. Subject 2's second smallest value would be 3, etc. Note that subject 4 has two zeros (a "tie") but if the diagonals are not in the equation, then the minimum value for this subject is from column 2.

Right now I coded off diagonals as missing and then order it that way but I feel like it's cheating. Suggestions??



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 04 Apr 2011 - 04:29:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Apr 2011 - 04:50:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive