Skip to main content
Version: Java (Groovy)

wavgBy

The wavgBy method returns a new table with a column containing the weighted average of a table's data column, grouped by the specified columns.

Syntax

table.wavgBy(weightColumn)
table.wavgBy(weightColumn, groupByColumns...)
table.wavgBy(weightColumn, groupByColumns)

Parameters

ParameterTypeDescription
weightColumnString

The column to use as the weight for the calculation.

groupByColumnsString...

The column(s) by which to group data.

  • NULL returns the weighted average for all non-key columns.
  • "X" will output the weighted average of each group in column X.
  • "X", "Y" will output the weighted average of each group designated from the X and Y columns.
groupByColumnsColumnName...

The column(s) by which to group data.

  • NULL returns the weighted average for all non-key columns.
  • "X" will output the weighted average of each group in column X.
  • "X", "Y" will output the weighted average of each group designated from the X and Y columns.
groupByColumnsCollection<String>

The column(s) by which to group data.

  • NULL returns the weighted average for all non-key columns.
  • "X" will output the weighted average of each group in column X.
  • "X", "Y" will output the weighted average of each group designated from the X and Y columns.

Returns

A new table with a column containing the weighted average of the data column, grouped by the specified columns.

Examples

In this example, wavgBy returns the weighted average of the Num1 column, with no grouping columns.

source = newTable(
intCol("Num1", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
intCol("Num2", 5, 4, 3, 2, 1, 1, 1, 1, 1, 1)
)

result = source.wavgBy("Num2")

In this example, wavgBy returns the weighted average of the Num1 column, grouped by the Letter column.

source = newTable(
stringCol("Letter", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B"),
intCol("Num1", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
intCol("Num2", 5, 4, 3, 2, 1, 1, 1, 1, 1, 1)
)

result = source.wavgBy("Num2", "Letter")