Search code examples
kdb

KDB: select and round off each row


I created my own function of round off:

.q.rnd:{$[x < 0; -1; 1] * floor abs[x] + 0.5}

I have a table Test with a string column of COL

select  "F"$(COL) from Test

24549.18741328
48939.50717263
-274853.33568872
-24549.18741328
298753.62574861
84822.70074144
-7468840.64371524
117944.21228603
-117944.21228603
7468840.64371524
-7468840.64371524

I want to derive a table that would round-off the records in Test

One would think that the statement below would work. But it does not.

select .q.rnd "F"$(COL) from Test

I get the error "type". So how do I round off the records?


Solution

  • The result if the if-else conditional must be an atomic boolean. When you run .q.rnd on a column, you are operating on a list and x<0 is going to return a list of booleans, not an atom. The vector conditional is ?

    Nonetheless, it looks like you want a resulting integer/long anyway, so just use parse here

    q)t:([]string (10?-1 1)*10?10000f)
    q)select  "F"$x from t
    x
    -------------------
    4123.1701336801052
    -9877.8444156050682
    -3867.3530425876379
    7267.8099689073861
    4046.5459413826466
    -8355.0649625249207
    6427.3701561614871
    -5830.2619284950197
    1424.9352994374931
    -9149.8820902779698
    q)select  "j"$"F"$x from t
    x
    -----
    4123
    -9878
    -3867
    7268
    4047
    -8355
    6427
    -5830
    1425
    -9150