kdb 中列表列的加权平均值

Weighted avgerage of list columns in kdb

如何从 kdb 中每行都有多个条目的其他 2 列计算一列加权平均值?

例如,给定以下 table:

T:([]sym:`a`b`c;size:(2 8;5 2 10;3 7);price:(1 2;1 1 10;2 4))

我想将列 (1.8 6.29 3.4) 添加到 table。

您可以对 table 中的每个嵌套列表使用 each-both adverb to apply the wavg 函数,例如

q)update x:wavg'[size;price] from T
sym size   price  x
--------------------------
a   2 8    1 2    1.8
b   5 2 10 1 1 10 6.294118
c   3 7    2 4    3.4