1
$\begingroup$

I have a dataset, where some data has to be ranked in regard to the number of ranking of the total dataset. I've made a simple example below:

names = {"Set A", "Set B", "Set C"};
values = {67, 80, 50};
{names, values} // Transpose

I would like to add a 3rd column, which described the rank of the total column. I've tried with RankedMax but without any luck. To be more exactly, the matrix below shows my problem, where the third column is ranking the highest to lowest value.

$$\left( \begin{array}{ccc} \text{Set 1} & 67 & 2 \\ \text{Set 2} & 80 & 1 \\ \text{Set 3} & 50 & 3 \\ \end{array} \right)$$

How can create such a column?

$\endgroup$
  • $\begingroup$ Does {names, values, Ordering[values]} // Transpose work to get what you need? $\endgroup$ – kglr May 18 '14 at 14:03
  • $\begingroup$ @kguler this is exactly what I needed. Very Simple I see. Thank you. Maybe I should delete this post? I suppose this will not add any value to others, since it is this simple... $\endgroup$ – Jens Jensen May 18 '14 at 14:08
  • $\begingroup$ @kguler I guess that should be Ordering[-values], given that the OP wants a descending sort $\endgroup$ – Sjoerd C. de Vries May 18 '14 at 14:09
  • $\begingroup$ @kguler After further investigation, I can see that Ordering doesn't solve the problem for all kinds of dataset, especially when they become larger than this example.. $\endgroup$ – Jens Jensen May 18 '14 at 14:26
  • $\begingroup$ Jens, what kind of issues do you get with Ordering for larger datasets? $\endgroup$ – kglr May 18 '14 at 14:34
2
$\begingroup$

The data set you pasted in your comment below contained several invisible non-ASCII characters.

They are revealed using ToCharacterCode

dataString = "{0.109892,0.153395,0.263218,0.0976219,0.26596,0.230077,0.235412,0.201642,0.07021‌​26,0.134909,0.155424,0.263319,0.224367,0.253994,0.117316,0.0733121,0.190985,0.305‌​341,0.139088,0.190262,0.00999462,0.20669,-0.00893785,0.087579,0.175154,0.0882176,‌​0.193825,0.236279,-0.0883605,0.229773,0.292539,0.0971533,0.0613749,0.298706,0.121‌​186,0.148439,0.000799421,0.284733,0.335953,-0.0610469,0.14451,0.0784617,0.136533,‌​0.37428,0.181673,0.262039,-0.217684,-0.13403,0.0359772,0.179099}"

ToCharacterCode[dataString]

(* {123, 48, 46, 49, 48, 57, 56, 57, 50, 44, 48, 46, 49, 53, etc. 49, 8204, \
8203, 50, 54, 44, etc. 48, 53, 8204, 8203,  etc. 44, 8204, 8203, 48, \
etc., 49, 8204, 8203, 49, 56, etc. 57, 125} *)

Convert to real ASCII:

data = DeleteCases[ToCharacterCode[dataString], _?(# > 127 &)] //FromCharacterCode // ToExpression
(* {0.109892, 0.153395, 0.263218, 0.0976219, 0.26596, 0.230077, \
0.235412, 0.201642, 0.0702126, 0.134909, 0.155424, 0.263319, \
0.224367, 0.253994, 0.117316, 0.0733121, 0.190985, 0.305341, \
0.139088, 0.190262, 0.00999462, 0.20669, -0.00893785, 0.087579, \
0.175154, 0.0882176, 0.193825, 0.236279, -0.0883605, 0.229773, \
0.292539, 0.0971533, 0.0613749, 0.298706, 0.121186, 0.148439, \
0.000799421, 0.284733, 0.335953, -0.0610469, 0.14451, 0.0784617, \
0.136533, 0.37428, 0.181673, 0.262039, -0.217684, -0.13403, \
0.0359772, 0.179099} *)

Ranking can be done using Ordering:

Ordering@Ordering@-data
(* {34, 26, 9, 35, 7, 14, 13, 18, 41, 31, 25, 8, 16, 11, 33, \
40, 20, 3, 29, 21, 44, 17, 46, 38, 24, 37, 19, 12, 48, 15, 5, 36, 42, \
4, 32, 27, 45, 6, 2, 47, 28, 39, 30, 1, 22, 10, 50, 49, 43, 23} *)

To show that this works, let's sort the paired data and ranking:

Transpose[{data, Ordering@Ordering@-data}] // Sort // Reverse
(* {{0.37428, 1}, {0.335953, 2}, {0.305341, 3}, {0.298706, 
  4}, {0.292539, 5}, {0.284733, 6}, {0.26596, 7}, {0.263319, 
  8}, {0.263218, 9}, {0.262039, 10}, {0.253994, 11}, {0.236279, 
  12}, {0.235412, 13}, {0.230077, 14}, {0.229773, 15}, {0.224367, 
  16}, {0.20669, 17}, {0.201642, 18}, {0.193825, 19}, {0.190985, 
  20}, {0.190262, 21}, {0.181673, 22}, {0.179099, 23}, {0.175154, 
  24}, {0.155424, 25}, {0.153395, 26}, {0.148439, 27}, {0.14451, 
  28}, {0.139088, 29}, {0.136533, 30}, {0.134909, 31}, {0.121186, 
  32}, {0.117316, 33}, {0.109892, 34}, {0.0976219, 35}, {0.0971533, 
  36}, {0.0882176, 37}, {0.087579, 38}, {0.0784617, 39}, {0.0733121, 
  40}, {0.0702126, 41}, {0.0613749, 42}, {0.0359772, 43}, {0.00999462,
   44}, {0.000799421, 45}, {-0.00893785, 46}, {-0.0610469, 
  47}, {-0.0883605, 48}, {-0.13403, 49}, {-0.217684, 50}} *)
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.