CWD
CWD

Reputation: 353

Weighted avgerage of list columns in kdb

How can I calculate a column of weighted averages from 2 other columns that both have multiple entries per row in kdb?

For example, given the following table:

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

I would like to add the column (1.8 6.29 3.4) to the table.

Upvotes: 4

Views: 1363

Answers (1)

Jonathon McMurray
Jonathon McMurray

Reputation: 2981

You can use the each-both adverb to apply the wavg function to each nested list in your table e.g.

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

Upvotes: 2

Related Questions