rank
The rank operator ranks array elements.
Synopsis
rank(array[, attribute][, dimension_1, dimension_2,...]]);
Summary
Ranking array elements sorts them and assigns an ordinal rank. The ordinal is a number that shows the position of the element in the series.
The avg_rank operator is equivalent to rank except for handling ties. The avg_rank operator averages the rank for the tied values. For details, see avg_rank.
Inputs
The rank operator takes the following arguments:
- array:Â A source array with one or more attributes and one or more dimensions.
- attribute_n:Â An optional attribute on which to sort. If no attributes are specified, the first one is used.
- dimension_n:Â An optional list of dimensions to group by. If no dimensions are specified, the ordering is global across the entire array.
Examples
Using the Operator
To demonstrate rank operator, do the following:Â
Create an array, by the name of A, by entering:
AFL% CREATE ARRAY A <a:int32>[x=0:9];
The output is:Query was executed successfullyÂ
Initialize the array A with data in the form of a sinewave:
AFL% store(build(A, sin(x)*100), A);Â
The output is:{x} a {0} 0 {1} 84 {2} 90 {3} 14 {4} -75 {5} -95 {6} -27 {7} 65 {8} 98 {9} 41Â
Rank the array element in A:
AFL% rank(A);Â
The output is:{x} a,a_rank {0} 0,4 {1} 84,8 {2} 90,9 {3} 14,5 {4} -75,2 {5} -95,1 {6} -27,3 {7} 65,7 {8} 98,10 {9} 41,6Â
Remove the array by entering:
AFL% remove(A);Â
The output is:Query was executed successfullyÂ
Rank an Array by Dimension
To rank a 4×4 array by dimension, do the following:
Assume the following 4×4 array called rank_array:
AFL% create array rank_array<val:int32>[i=0:3; j=0:3];
The output is:
Query was executed successfullyÂ
Initialize the data in the array
store(build(rank_array, '[[9,1,0,6],[1,3,7,7],[2,3,9,8],[5,9,5,9]]', true), rank_array);
The output is:
{i,j} val {0,0} 9 {0,1} 1 {0,2} 0 {0,3} 6 {1,0} 1 {1,1} 3 {1,2} 7 {1,3} 7 {2,0} 2 {2,1} 3 {2,2} 9 {2,3} 8 {3,0} 5 {3,1} 9 {3,2} 5 {3,3} 9
Rank the elements in rank_array by dimension i:
AFL% rank(rank_array,val,i);Â
The output is:{i,j} val,val_rank {0,0} 9,4 {0,1} 1,2 {0,2} 0,1 {0,3} 6,3 {1,0} 1,1 {1,1} 3,2 {1,2} 7,3 {1,3} 7,3 {2,0} 2,1 {2,1} 3,2 {2,2} 9,4 {2,3} 8,3 {3,0} 5,1 {3,1} 9,3 {3,2} 5,1 {3,3} 9,3
Notice that the first value for each cell is the value of the val attribute, and the second value is the rank. Ties are shown in green.
ÂRank all the elements of the array:
ÂAFL% rank(rank_array,val);Â
Â
The output is:
Â{i,j} val,val_rank {0,0} 9,13 {0,1} 1,2 {0,2} 0,1 {0,3} 6,9 {1,0} 1,2 {1,1} 3,5 {1,2} 7,10 {1,3} 7,10 {2,0} 2,4 {2,1} 3,5 {2,2} 9,13 {2,3} 8,12 {3,0} 5,7 {3,1} 9,13 {3,2} 5,7 {3,3} 9,13
Notice that the four occurrences of '9' are all ranked the same, as '13', which is the highest rank in the data set.
Remove the array by entering:
AFL% remove(rank_array);
The output is:
Query was executed successfullyÂ
Â
Â