RANKS [DESCENDING] inputVector resultVariable

Replaces the result vector with a list of the ranks of the elements of the input vector. If more than one element has the same rank, the rank of those elements is given the value midway between the highest and lowest ranks that that value occupies. In the example at the right, the two elements with value 4 occupy ranks 4 and 5. Therefore, they are given the rank (4+5)/2 = 4.5.

By default, the lowest number in the input vector is given the rank 1. If the keyword descending is present, the highest number will be given the rank 1.

Missing data, represented by "NaN" or ".", is assumed to be larger than the largest value than can be expressed. Therefore NaNs will rank at the high end in the ascending case and at the low end in the descending case. If you want to remove NaNs from your vectors, use the CLEAN command.

In the process of determining ranks, the RANKS command must sort its input vector. RANKS will perform its sort into the same order that the SORT command would use. See the second example at right for a look at how RANKS handles mixed vectors (vectors containing numbers and Named Constants). For a full explanation of the sorting of mixed vectors, see the SORT command.

Usage with the LET command

You can use RANKS as a function in the LET command. By default, it will rank a vector's elements in ascending order. If you want them in descending order, use the name RANKS_DESCENDING, which is only valid within a LET command.

COPY (5 3 NaN 2 4 1 4) A
RANKS A B
RANKS descending A C
PRINT A B C

The above program produces the following output:

A: (5.0 3.0 NaN 2.0 4.0 1.0 4.0)
B: (6.0 3.0 7.0 2.0 4.5 1.0 4.5)
C: (2.0 5.0 1.0 6.0 3.5 7.0 3.5)

Next is an example showing how the RANKS command handles mixed vectors:

NAME Jan Feb Mar               'Doesn't give values to these names
NAME 1,3 Ace Deuce Trey        'Gives values to these names
'Make a mixed vector:
COPY 1,3 jan,mar Ace,Trey vec  'Numbers and both kinds of names
'Sort vec to show how the different
'types interact:
SORT vec vecSorted
RANKS vec vecRanks
PRINT table vec vecRanks vecSorted

Here is the result:

vec           	vecRanks      	vecSorted
1             	4.5           	Jan
2             	6.5           	Feb
3             	8.5           	Mar
Jan           	1             	1
Feb           	2             	Ace
Mar           	3             	2
Ace           	4.5           	Deuce
Deuce         	6.5           	3
Trey          	8.5           	Trey