phplyr

Summarize groups

Count the penguins, and find the mean and largest body mass, for each species, then sort the species by mean mass. penguins has size measurements for 344 penguins near Palmer Station, Antarctica, from 2007 to 2009 (Gorman et al., 2014).

use Phibble\DataFrame;use function Phplyr\{group_by, summarize, arrange, desc};use function Phplyr\{n, mean, max};$data = 'https://raw.githubusercontent.com/phpstats'    . '/datasets/main/csv';$penguins = DataFrame::fromCsv("$data/penguins.csv");echo $penguins |>    group_by('species') |>    summarize(        penguins: n(),        avg_mass_g: mean('body_mass_g', naRm: true),        max_mass_g: max('body_mass_g', naRm: true),    ) |>    arrange(desc('avg_mass_g'));
use Phibble\DataFrame;use function Phplyr\{phplyr, desc, n, mean, max};$data = 'https://raw.githubusercontent.com/phpstats'    . '/datasets/main/csv';$penguins = DataFrame::fromCsv("$data/penguins.csv");echo phplyr($penguins)    ->groupBy('species')    ->summarize(        penguins: n(),        avg_mass_g: mean('body_mass_g', naRm: true),        max_mass_g: max('body_mass_g', naRm: true),    )    ->arrange(desc('avg_mass_g'));
# A tibble: 3 × 4
  species   penguins avg_mass_g max_mass_g
  <chr>        <int>      <dbl>      <int>
1 Gentoo         124      5076.       6300
2 Chinstrap       68      3733.       4800
3 Adelie         152      3701.       4775
  • summarize() after group_by('species') returns one row per species.
  • n() counts every row in the group, including the Adelie and the Gentoo penguin with no recorded mass.
  • naRm: true leaves missing values out of mean() and max(). Without it, the Adelie and Gentoo results are NA, as in R.
  • Gentoo penguins are the heaviest, with a mean of about 5,076 g.

Reference: Single-table functions · Grouping · Vector helpers