James
James

Reputation: 1260

KDB+/Q:How to efficiently fill values (with zeros) whereby the column names conform to a given list?

Given the following dictionart (98h):

q) d
eid     kind      value1     value2   value3
------------------------------------------------------
0       `D        0          1        9
1       `A        1          0        0N

How can one perform (modify/structure) the following selection:

q) d[`eid`kind`value1`value2`value3`value4] //thanks for correction

which returns:

 0  1
`D `A
 0  1
 1  0
 9  0N
 `long$()

such that the selection returns the following:

 0  1
`D `A
 0  1
 1  0
 9  0
 0  0

i.e. the nulls in all forms i.e. column missing and null values are replaced with 0;

I have tried using where however this doesn't work for missing columns i.e.

 00b
 00b
 00b
 00b
 01b
 `boolean$()

I have also tried using fill i.e. 0^.

Edit: The main problem here is that I have zero prior knowledge of the matrix that is derived from the table i.e. it could be like the following

 00b
 01b
 00b
 `boolean$()
 00b
 00b

What is the best method to achieve this functionality? Thanks in advance for your guidance.

Upvotes: 0

Views: 922

Answers (2)

joeyg567
joeyg567

Reputation: 86

Your first line you specify you have a dictionary of type 98h, a dictionary has type 99h. Your example of d looks like it is a table which has type 98h. Is the below what you are looking to achieve(where d is a dictionary)?

q)d: `eid`kind`value1`value2`value3!(0 1;`D`A;0 1; 1 0; 9 0N)
q)update 0^value3 from d
eid   | 0 1
kind  | D A
value1| 0 1
value2| 1 0
value3| 9 0

Upvotes: 2

terrylynch
terrylynch

Reputation: 13657

You seem to want to do two things here: fill nulls in a given column, and generate a new column if it doesn't already exist. This can achieve both:

q)d:([]eid:0 1;kind:`D`A;value1:0 1;value2:1 0;value3:9 0N)
q)@[;;{$[count x;y^x;y]};0]/[d;`value3`value4]
eid kind value1 value2 value3 value4
------------------------------------
0   D    0      1      9      0
1   A    1      0      0      0

Upvotes: 1

Related Questions