groupby (:G)
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 are the parameters of func. It is a tuple if there are more than 1 parameter of func.
groupingCol is the grouping variable. groupingCol and each of the function argument in funcArgs are vectors of the same size.
Details
Calculate func(funcArgs)
for each groupingCol group. The
result for each group can be a scalar/vector/dictionary, and the output of the
template is a table of the same size as the number of groupingCol 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 |