Skip to contents

Creates a Weighted Average aggregation that computes the weighted average of each column in cols for each aggregation group.

Arguments

wcol

String denoting the column to use for weights. This must be a numeric column.

cols

String or list of strings denoting the column(s) to aggregate. Can be renaming expressions, i.e. “new_col = col”. Default is to aggregate all non-grouping columns, which is only valid in the agg_all_by() operation.

Value

AggOp to be used in a call to agg_by() or agg_all_by().

Details

The aggregation groups that this function acts on are defined with the by parameter of the agg_by() or agg_all_by() caller function. The aggregation groups are defined by the unique combinations of values in the by columns. For example, if by = c("A", "B"), then the aggregation groups are defined by the unique combinations of values in the A and B columns.

This function, like other Deephaven agg functions, is a generator function. That is, its output is another function called an AggOp intended to be used in a call to agg_by() or agg_all_by(). This detail is typically hidden from the user. However, it is important to understand this detail for debugging purposes, as the output of an agg function can otherwise seem unexpected.

For more information, see the vignette on agg functions by running vignette("agg_by").

Examples

if (FALSE) { # \dontrun{
library(rdeephaven)

# connecting to Deephaven server
client <- Client$new("localhost:10000", auth_type = "psk", auth_token = "my_secret_token")

# create data frame, push to server, retrieve TableHandle
df <- data.frame(
  X = c("A", "B", "A", "C", "B", "A", "B", "B", "C"),
  Y = c("M", "N", "O", "N", "P", "M", "O", "P", "M"),
  Number1 = c(100, -44, 49, 11, -66, 50, 29, 18, -70),
  Number2 = c(-55, 76, 20, 130, 230, -50, 73, 137, 214)
)
th <- client$import_table(df)

# compute weighted average of Number1, weighted by Number2
th1 <- th$
  agg_by(agg_w_avg(wcol = "Number2", cols = "Number1"))

# compute weighted average of Number1, weighted by Number2, grouped by X
th2 <- th$
  agg_by(agg_w_avg(wcol = "Number2", cols = "Number1", by = "X"))

# compute weighted average of Number1, weighted by Number2, grouped by X and Y
th3 <- th$
  agg_by(agg_w_avg(wcol = "Number2", cols = "Number1", by = c("X", "Y")))

client$close()
} # }