groupby (:G)

Syntax

groupby(func, funcArgs, groupingCol)

or

funcArg func:G groupingCol

Arguments

func is a function.

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.

For the second use case, func can only have one parameter ( funcArg ).

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

$ groupby(wavg, [price, qty], sym);

sym

avg_price

IBM

173.856129

MS

28.373869