# Re: \[personal kdb+\] 'rank error

**URL:** <https://forum.kx.com/t/re-personal-kdb-rank-error/7902>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [October 1, 2012, 7:54am UTC](https://forum.kx.com/t/re-personal-kdb-rank-error/7902 "2012-10-01T07:54:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rory\_ororke](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@rory\_ororke](https://forum.kx.com/u/rory_ororke)\
**Post date:** [October 1, 2012, 7:54am UTC](https://forum.kx.com/t/re-personal-kdb-rank-error/7902/1 "2012-10-01T07:54:00Z")

</div>

Hi Rory,

You have to make the key of the dictionary into symbol format and also the rank error is due to the value d being all atoms and not list so these must be enlisted.

q)d:{count each group y xbar x}

q)x:1000?100

q)k:d[x;10]

q)k

50| 91

20| 101

30| 119

10| 118

70| 106

40| 99

80| 92

90| 84

60| 108

0 | 82

q)tab:flip(`$(string key k))!enlist each value k

q)tab

50 20 ?30 ?10 ?70 ?40 80 90 60 ?0

* * *

91 101 119 118 106 99 92 84 108 82

q)type tab

98h

Hope this helps.

Thanks Rory

---

<div class="post-metadata">

**Author:** ![pauljloughran](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pauljloughran](https://forum.kx.com/u/pauljloughran)\
**Post date:** [October 1, 2012, 12:24pm UTC](https://forum.kx.com/t/re-personal-kdb-rank-error/7902/2 "2012-10-01T12:24:00Z")

</div>

Hi Guys,

Perhaps a slightly more terse solution would be;

## q)tab:enlist (`$string key k)!value k q)tab 10? 0?? 90 70? 40? 80 60? 50? 30? 20

104 100 99 101 100 89 100 109 109 89  
q)type tab  
98h

Kind regards,

Paul

---

<div class="post-metadata">

**Author:** ![petrum1](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@petrum1](https://forum.kx.com/u/petrum1)\
**Post date:** [October 1, 2012, 9:31pm UTC](https://forum.kx.com/t/re-personal-kdb-rank-error/7902/3 "2012-10-01T21:31:00Z")

</div>

Also if you change the 'd' function to:

d:{count each group `$string y xbar x}

you get a even shorter solution:

## q)enlist d[x;10] 30 0&nbsp; 50&nbsp; 60&nbsp; 70 90&nbsp; 40&nbsp; 80&nbsp; 20 10

87 97 114 111 83 110 105 103 97 93

Regards,  
Petru

---

<div class="post-metadata">

**Author:** ![Rory\_Winston](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@Rory\_Winston](https://forum.kx.com/u/Rory_Winston)\
**Post date:** [October 7, 2012, 11:28am UTC](https://forum.kx.com/t/re-personal-kdb-rank-error/7902/4 "2012-10-07T11:28:00Z")

</div>

Sorry - just as a final coda to this topic - I had neglected to sort the bins, so the function now looks like:

d:{h:count each group y xbar x; (`$(string asc key h))!(enlist each h[asc key h])}

Can anyone make this more compact / elegant? My approach looks a bit clunky to my eyes..

---

<div class="post-metadata">

**Author:** ![pauljloughran](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pauljloughran](https://forum.kx.com/u/pauljloughran)\
**Post date:** [October 7, 2012, 1:44pm UTC](https://forum.kx.com/t/re-personal-kdb-rank-error/7902/5 "2012-10-07T13:44:00Z")

</div>

Hi,

This should do it.

## q)x:1000?100 q)d:{h:count'[group y xbar x];(`$string a)!h a:asc key h} q)enlist d[x;10] 0? 10 20 30 40? 50 60? 70? 80? 90

99 96 95 98 101 98 100 102 115 96

Regards,

Paul
