s-news
[Top] [All Lists]

Ranking rows of a matrix

To: <s-news@wubios.wustl.edu>
Subject: Ranking rows of a matrix
From: David.Jessop@ubsw.com
Date: Fri, 25 Oct 2002 16:57:12 +0100
Thread-index: AcJ8Py+taBraEegMEda5uAAGWwHaZg==
Thread-topic: Ranking rows of a matrix
Hi,

I have a problem trying to rank across the rows of a matrix where in each row I 
might not want to include every element.   For example, I have a 3x3 matrix (in 
actual fact much bigger) called fred (it's better than test!) :

fred = 
            [,1]      [,2]       [,3] 
[1,]  0.09616424 -1.194590  0.2640956
[2,] -0.35525355  1.500998 -0.1802140
[3,]  0.44401227  1.059076  0.7822802

and another matrix showing the elements I am interested in in each row:

in.fred = 
     [,1] [,2] [,3] 
[1,]    F    T    T
[2,]    T    T    F
[3,]    T    F    T

what I want to generate is a matrix of the ranks of the rows

comp.rank
     [,1] [,2] [,3] 
[1,]   NA    1    2
[2,]    1    2   NA
[3,]    1   NA    2

The best I've come up with is:

 for (i in 1:3)
        {
        comp.rank [i, in.fred [i,]] = rank (fred [i, in.fred [i,]])
        }

I'm sure that there must be a better way than this but I can't see it.  Note 
that I can't rely on there being the same number of trues in each line of the 
in.fred matrix.  Any help gratefully received. 

Regards,

David Jessop

Visit our website at http://www.ubswarburg.com

This message contains confidential information and is intended only
for the individual named.  If you are not the named addressee you
should not disseminate, distribute or copy this e-mail.  Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.

E-mail transmission cannot be guaranteed to be secure or error-free
as information could be intercepted, corrupted, lost, destroyed,
arrive late or incomplete, or contain viruses.  The sender therefore
does not accept liability for any errors or omissions in the contents
of this message which arise as a result of e-mail transmission.  If
verification is required please request a hard-copy version.  This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities or
related financial instruments.


<Prev in Thread] Current Thread [Next in Thread>