Aggregate with expressions
Summarize each penguin species with several expressions in one agg() call.
The data are body measurements of 344 penguins near Palmer Station, Antarctica, from the R palmerpenguins package (penguins), read by URL.
Requires the phpolars extension (Getting started). Runnable script: expressions.php.
use function Phpolars\{col, len, read_csv};$data = 'https://raw.githubusercontent.com' .'/phpstats/datasets/main/csv';$penguins = read_csv( "$data/penguins.csv", nullValues: 'NA',);$bySpecies = $penguins ->groupBy('species', maintainOrder: true) ->agg( len()->alias('penguins'), col('body_mass_g')->mean()->round() ->alias('mass_g'), col('body_mass_g') ->filter(col('sex')->eq('female')) ->mean() ->round() ->alias('female_g'), col('island')->nUnique()->alias('islands'), col('island') ->sortBy( 'body_mass_g', descending: true, nullsLast: true, ) ->first() ->alias('heaviest_on'), );echo $bySpecies;shape: (3, 6)
┌───────────┬──────────┬────────┬──────────┬─────────┬─────────────┐
│ species ┆ penguins ┆ mass_g ┆ female_g ┆ islands ┆ heaviest_on │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ u32 ┆ f64 ┆ f64 ┆ u32 ┆ str │
╞═══════════╪══════════╪════════╪══════════╪═════════╪═════════════╡
│ Adelie ┆ 152 ┆ 3701.0 ┆ 3369.0 ┆ 3 ┆ Biscoe │
│ Gentoo ┆ 124 ┆ 5076.0 ┆ 4680.0 ┆ 1 ┆ Biscoe │
│ Chinstrap ┆ 68 ┆ 3733.0 ┆ 3527.0 ┆ 1 ┆ Dream │
└───────────┴──────────┴────────┴──────────┴─────────┴─────────────┘
nullValues: 'NA'reads the file’sNAentries as nulls.mean()skips them, somass_gaverages the penguins with a recorded body mass.filter()inside an aggregate limits only that aggregate.female_gaverages female penguins, andpenguinsstill counts every penguin of the species.sortBy('body_mass_g', ...)->first()picks a value by another column’s order.heaviest_onis the island of the species’ heaviest penguin.nullsLast: truekeeps penguins without a body mass out of first place.- Without
maintainOrder: true, the order of the groups can change between runs.
Reference: groupBy() · agg() · Expressions