Search code examples
kdb

KDB replace null integer in all columns with int or float


Expanding this question,

kdb+: replace null integer with 0

Ff I have a large table that contains different data types like float, int, timestamps, how can I expand this

0^tbl

to only get applied to columsn of a certain type. Currently, if I apply it to the full table, I get a type error and there must eb a smarter way than doing it column by column manually


Solution

  • You can do this using a functional apply based on the type of each column, something like the following:

    q)t:([] a:9?`3;b:9#0N;c:9#0Nf;d:9#0Ni)
    q)@[t;where (type each flip t) within 5 9h;0^]
    a   b c d
    ---------
    kdj 0 0 0
    eeg 0 0 0
    nce 0 0 0
    jog 0 0 0
    ..
    

    This works by applying 0^ to only the columns with types between 5 and 9 (i.e. short, int, long, real and float)

    Hope this helps

    Jonathon