groupby
Syntax
groupby(func, funcArgs, groupingCol)
or
funcArg func:G groupingCol
or
func:G(funcArgs, groupingCol)
Arguments
func is a function. For the second use case, func can only have one parameter (funcArg).
funcArgs is a vector or a tuple with multiple vectors specifying the arguments of func.
groupingCol is a vector or a tuple with vectors of the same length indicating the grouping column(s). A grouping column and each argument in funcArgs are vectors of the same size.
Details
For each group, calculate func(funcArgs)
and return a
scalar/vector/dictionary.
Return value: A table, where the number of rows is the same as the number of groups.
Examples
sym=`IBM`IBM`IBM`MS`MS`MS$symbol;
price=172.12 170.32 175.25 26.46 31.45 29.43;
qty=5800 700 9000 6300 2100 5300;
trade_date=2013.05.08 2013.05.06 2013.05.07 2013.05.08 2013.05.06 2013.05.07;
groupby(avg, price, sym);
sym | avg_price |
---|---|
IBM | 172.563333 |
MS | 29.113333 |
price avg :G sym;
sym | avg_price |
---|---|
IBM | 172.563333 |
MS | 29.113333 |
// calculate the weighted average price of each stock
groupby(wavg, [price, qty], sym);
sym | avg_price |
---|---|
IBM | 173.856129 |
MS | 28.373869 |
sym = `C`MS`MS`MS`IBM`IBM`C`C`C$SYMBOL
price= 49.6 29.46 29.52 30.02 174.97 175.23 50.76 50.32 51.29
qty = 2200 1900 2100 3200 6800 5400 1300 2500 8800
timestamp = [09:34:07,09:36:42,09:36:51,09:36:59,09:32:47,09:35:26,09:34:16,09:34:26,09:38:12]
groupby(max, price, [sym,minute(timestamp)])
sym | groupingKey | max_price |
---|---|---|
C | 09:34m | 50.76 |
C | 09:38m | 51.29 |
IBM | 09:32m | 174.97 |
IBM | 09:35m | 175.23 |
MS | 09:36m | 30.02 |