Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

filtering columns that have lists for rows based on other scalar columns in kdb

w:(1 2;3 4;5 2); l:til count w; h:l+1;
show t:([] l; h; w);

l h w  
-------
0 1 1 2
1 2 3 4
2 3 5 2

I have a table t with a column w which has lists for rows. I would like to only keep elements in each row of w which lie between the l and h values for each row of the table.

The expected output is:

l h w  
-------
0 1 ,1
1 2 ,
2 3 ,2

I tried doing

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

t:update w:w[where ((w>=l) and (w<=h))] from t;

but it fails with

'type
  [0]  t:update w:w[where ((w>=l) and (w<=h))] from t;

>Solution :

You can use within (https://code.kx.com/q/ref/within)

q)update w:w@'where each w within(l;h)from t
l h w
------------
0 1 ,1
1 2 `long$()
2 3 ,2

Your original attempt was close, here’s the corrected version:

q)update w:w@'where each(w>=l)and w<=h from t
l h w
------------
0 1 ,1
1 2 `long$()
2 3 ,2

You needed to consider the table columns as lists (so you need the each and each-both (') iterators.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading