g_wavg(G;S;X;Y)

Returns the weighted average of values within a given group.

Function type

Vector only

Syntax

g_wavg(G;S;X;Y)

Input

Argument Type Description
G any A space- or comma-separated list of column names

Rows are in the same group if their values for all of the columns listed in G are the same.

If G is omitted, all rows are considered to be in the same group.

If any of the columns listed in G contain N/A, the N/A value is considered a valid grouping value.

S integer The name of a column in which every row evaluates to a 1 or 0, which determines whether or not that row is selected to be included in the calculation

If S is omitted, all rows will be considered by the function (subject to any prior row selections).

If any of the values in S are neither 1 nor 0, an error is returned.

X integer or decimal A column name

Rows where X and/or Y are N/A are ignored (i.e., the result is the weighted average of the remaining rows).

Y integer or decimal A column name containing the weights to apply to the values in X when calculating the weighted average

Rows where X and/or Y are N/A are ignored (i.e., the result is the weighted average of the remaining rows).

Return Value

For every row in each group defined by G (and for those rows where S=1, if specified), g_wavg returns a decimal value corresponding to the weighted average of the values in the column listed in X that are in the same group as that row. The values in X are weighted by the values in the column listed in Y.

If no rows have valid (non-N/A) values for both X and Y, the result is N/A.

Sample Usage

<base table="pub.doc.samples.ref.func.g_func_sample_usage_two_values"/>
<willbe name="g_wavg_1" value="g_wavg(state;include;value1;value2)"/>
<willbe name="g_wavg_2" value="g_wavg(state city;include;value1;value2)"/>

Example

This example uses the g_wavg(G;S;X;Y) function to return the weighted average of all three stores (total_wavg). The g_wavg(G;S;X;Y) function can take four parameters, but in this case only the X and Y parameters are needed. The X parameter is each store's average cost (cost), and the Y parameter is the amount the average is weighted by.

<base table="pub.demo.retail.item"/>
<colord cols="transid,date,store,cost"/>
<willbe name="num_stores" value="g_cnt(store;)" label="Number of`Stores"/>
<willbe name="avg_per_store" value="g_avg(store;;cost)" format="dec:2"/>
<willbe name="total_wavg" value="g_wavg(;;avg_per_store;num_stores)" 
 format="dec:2"/>