Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kdb apply function in select by row

Tags:

kdb

I have a table

t: flip `S`V ! ((`$"|A|B|"; `$"|B|C|D|"; `$"|B|"); 1 2 3)

and some dicts

t1: 4 10 15 20 ! 1 2 3 5;
t2: 4 10 15 20 ! 0.5 2 4 5;

Now I need to add a column with values on the the substrings in S and the function below (which is a bit pseudocode because I am stuck here).

f:{[s;v];
    if[`A in "|" vs string s; t:t1;];
    else if[`B in "|" vs string s; t:t2;];
    k: asc key t;
    :t k k binr v;
}

problems are that s and v are passed in as full column vectors when I do something like

update l:f[S,V] from t;

How can I make this an operation that works by row? How can I make this a vectorized function? Thanks

like image 256
chrise Avatar asked Feb 13 '18 06:02

chrise


2 Answers

You will want to use the each-both adverb to apply a function over two columns by row.

In your case:

update l:f'[S;V] from t;
like image 154
wpc135 Avatar answered Oct 08 '22 17:10

wpc135


To help with your pseudocode function, you might want to use $, the if-else operator, e.g.

f:{[s;v]
  t:$["A"in ls:"|"vs string s;t1;"B"in ls;t2;()!()];
  k:asc key t;
  :t k k binr v;
 };

You've not mentioned a final else clause in your pseudocode but $ expects one hence the empty dictionary at the end.

Also note that in your table the columns S and V have been cast to a symbol. vs expects a string to split so I've had to use the stringoperation - this could be removed if you are able to redefine your original table.

Hope this helps!

like image 23
Jemma Borland Avatar answered Oct 08 '22 18:10

Jemma Borland