Search code examples
kdb

Apply step function to values


I have the following table:

t:([]sym:`eae`oco`khd`dga`eob`iog`edg`kio`gme`iko; val:1 0 5 1 0 0 1 7 6 3)

sym val
-------
eae 1  
oco 0  
khd 5  
dga 1  
eob 0  
iog 0  
edg 1  
kio 7  
gme 6  
iko 3  

What I want is to assign some bucket value according to the following rules:

if 0 <= val < 1, then bucket = 3 
if 1 <= val < 3, then bucket = 8
if 3 <= val < 5, then bucket = 20
if 5 <= val    , then bucket = 30

So similar to applying a step function to the values val. The resulting table should look like this:

sym val bucket
--------------
eae 1   8     
oco 0   3     
khd 5   30    
dga 1   8     
eob 0   3     
iog 0   3     
edg 1   8     
kio 7   30    
gme 6   30    
iko 3   20  

Of course, I don't want to apply if[if[...]], and I am looking for an elegant q-ist solution.


Solution

  • The following works:

    update bucket: (`s#0 1 3 5!3 8 20 30)[val] from t