phibble

Grouping and summaries

Methods that set groups, summarize them, and run operations per group.

Grouping

->groupBy()

$table->groupBy(array $names, bool $drop = true): Table

Set the grouping columns, replacing any existing grouping.

Grouping is metadata: filter(), mutate(), and summarise() work within groups, and other methods keep the grouping.

Parameter Type Default Description
$names list<string> Grouping columns.
$drop bool true dplyr’s .drop. With false, grouped operations also visit empty groups: every factor level without rows. Other key columns contribute their observed values. The setting survives row operations; ungroup() and groupBy() reset it.

Method of DataFrame, SqlTable, PolarsTable
Returns Table
Examples Getting started, Reading and writing data, Expressions

->ungroup()

$table->ungroup(): Table

Remove the grouping, including rowwise grouping.

Method of DataFrame, SqlTable, PolarsTable
Returns Table

->groups()

$table->groups(): array

The grouping columns. Empty when ungrouped; the id columns of a rowwise table.

Method of DataFrame, SqlTable, PolarsTable
Returns list<string>

->dropsEmptyGroups()

$table->dropsEmptyGroups(): bool

Whether empty groups are dropped. False after groupBy($names, drop: false).

Method of DataFrame, SqlTable, PolarsTable
Returns bool

->ngroups()

$table->ngroups(): int

Number of groups (1 for an ungrouped table, nrow for a rowwise one).

Method of DataFrame
Returns int

->rowwise()

$table->rowwise(array $idColumns = []): Table

Treat each row as its own group, as dplyr’s rowwise(). groupBy() and ungroup() clear it.

Parameter Type Default Description
$idColumns list<string> [] Columns summarise() keeps, as with group keys.

Method of DataFrame, SqlTable, PolarsTable
Returns Table

->isRowwise()

$table->isRowwise(): bool

Whether the table is rowwise.

Method of DataFrame, SqlTable, PolarsTable
Returns bool

Summaries

->summarise()

$table->summarise(array $aggregates, ?string $groups = null): Table

One row per group: the group keys, then one column per entry.

Groups are sorted, with NA last. Each entry is a closure given the group’s table, or an Expr that evaluates to one value, usually an aggregate. The result drops the last grouping column, as in dplyr; a rowwise table keeps its id columns as groups.

Parameter Type Default Description
$aggregates array<string, Closure(Table): mixed|Expr> New column name => closure or expression.
$groups string|null null dplyr’s .groups: ‘drop_last’ (the default), ‘drop’, ‘keep’, or ‘rowwise’. Rowwise tables default to ‘keep’.

Method of DataFrame, SqlTable, PolarsTable
Returns Table
Throws LengthException: when an expression gives more than one value per group.

->summarize()

$table->summarize(array $aggregates, ?string $groups = null): static

Alias of summarise().

Parameter Type Default Description
$aggregates array
$groups ?string null

Method of DataFrame, SqlTable, PolarsTable
Returns static
Examples Getting started, Reading and writing data, Expressions

Per-group operations

->filterGroups()

$table->filterGroups(Closure $predicate): Table

Keep rows by a predicate that runs once per group.

The closure receives the group’s table and returns one value per row; a truthy value keeps the row.

Parameter Type Default Description
$predicate Closure(Table): list<?bool>

Method of DataFrame, SqlTable, PolarsTable
Returns Table

->mutateGroups()

$table->mutateGroups(array $columns): Table

Add or replace columns with closures that run once per group.

Each closure receives the group’s table and returns a list with one value per row of the group, or a scalar to repeat. Other values behave as in mutate(). Window functions and across() build on this method.

Parameter Type Default Description
$columns array<string, Closure(Table): (list<mixed>|scalar|null)|list<mixed>|scalar|null> New column name => value.

Method of DataFrame, SqlTable, PolarsTable
Returns Table

->eachGroup()

$table->eachGroup(): iterable

Each group’s key and rows as an ungrouped table, in sorted key order.

Rowwise tables yield one group per row, in row order. An ungrouped table yields one group with an empty key, also when it has no rows.

Method of DataFrame, SqlTable, PolarsTable
Returns iterable<int, array{key: array<string, mixed>, table: Table}>

->groupRows()

$table->groupRows(): array

Each group’s key and 0-based row indexes, in the order of eachGroup().

Empty groups (drop: false) have no rows; a rowwise table has one group per row.

Method of DataFrame, SqlTable, PolarsTable
Returns list<array{key: array<string, mixed>, rows: list<int>}>