aggregate_entity_values: Aggregate rated entity values to parent records

View source: R/entities.R

aggregate_entity_valuesR Documentation

Aggregate rated entity values to parent records

Description

Aggregate one or more numeric values from entity-level records to a parent or group level. This can be used, for example, to average driver factors or sum premiums for boats or scheduled items.

Usage

aggregate_entity_values(
  rated_entity_data,
  group_col,
  value_cols,
  aggregation = "mean",
  weight_col = NULL,
  output_names = NULL,
  output_prefix = NULL
)

Arguments

rated_entity_data

A data frame containing rated entity records.

group_col

A character string naming the column that identifies the parent or aggregation group.

value_cols

A character vector naming the numeric columns to aggregate.

aggregation

A character string specifying the aggregation method. Supported values are "sum", "mean", "min", "max", "count", and "weighted_mean".

weight_col

An optional character string naming the weight column. Required when aggregation = "weighted_mean".

output_names

An optional character vector giving the names of the aggregated output columns. It must have the same length as value_cols.

output_prefix

An optional character string prepended to generated output names when output_names is not supplied. By default, the aggregation name followed by an underscore is used.

Details

Common "sum", "mean", and "count" aggregations are performed in grouped vectorized passes rather than repeatedly subsetting the entity data once per parent group. Other supported aggregations reuse a single set of precomputed group indices.

Value

A data frame with one row per unique value of group_col and one aggregated column for each entry in value_cols.

Examples

rated_drivers <- data.frame(
  policy_id = c("P1", "P1", "P2"),
  indicated_BI = c(1.10, 0.90, 1.05)
)

aggregate_entity_values(
  rated_entity_data = rated_drivers,
  group_col = "policy_id",
  value_cols = "indicated_BI",
  aggregation = "mean",
  output_names = "average_BI"
)

ratingtables documentation built on Sept. 6, 2026, 1:06 a.m.