Search by

phpstats / phplyr

erikwestlund

A dplyr-style grammar of data manipulation for PHP 8.3+, with method chains or PHP 8.5 pipes

Package info

github.com/phpstats/phplyr

pkg:composer/phpstats/phplyr

Statistics

Installs: 44

Dependents: 1

Suggesters: 1

Stars: 0

Open Issues: 0

v0.4.3 2026-10-31 16:42 UTC

This package is auto-updated.

Last update: 2026-10-31 19:53:55 UTC


README

phplyr is a PHP library for filtering, transforming, summarizing, joining, and reshaping tables of data. Its functions and arguments follow R's dplyr and tidyr, and it works on phibble tables: the in-memory DataFrame, database tables, and Polars through phibble's PolarsTable.

To run the example below, create a Composer project in an empty directory, download the data, save one of the scripts as first.php, and run php first.php:

composer require phpstats/phplyr
curl -O https://raw.githubusercontent.com/phpstats/phplyr/v0.4.3/docs/data/schools.csv

PHP 8.5+

<?php

require __DIR__.'/vendor/autoload.php';

use Phibble\DataFrame;
use function Phplyr\{filter, group_by, summarize, arrange, head, desc, n, mean, sd};

$schools = DataFrame::fromCsv('schools.csv');

echo $schools |>
    filter(fn ($r) => $r->study_hours >= 5) |>
    group_by('school') |>
    summarize(students: n(), avg_score: mean('score'), sd_score: sd('score')) |>
    arrange(desc('avg_score')) |>
    head(5);

PHP 8.3–8.4

<?php

require __DIR__.'/vendor/autoload.php';

use Phibble\DataFrame;
use function Phplyr\{phplyr, desc, n, mean, sd};

$schools = DataFrame::fromCsv('schools.csv');

echo phplyr($schools)
    ->filter(fn ($r) => $r->study_hours >= 5)
    ->groupBy('school')
    ->summarize(students: n(), avg_score: mean('score'), sd_score: sd('score'))
    ->arrange(desc('avg_score'))
    ->head(5);

Installation

composer require phpstats/phplyr

phplyr supports PHP 8.3, 8.4, and 8.5, and requires the mbstring and PDO extensions. CI tests all three PHP versions. The pipe operator needs PHP 8.5.

The statistical models need the phit extension or phit-polyfill. Installation describes the requirements and the optional features.

Documentation

The documentation is at https://phplyr.phpstats.org/docs:

CHANGELOG.md lists the changes in each release.

Development

composer install
bin/test                        # uses .ext/libphit.dylib when present
NO_EXT=1 bin/test               # without phit
PHP=/path/to/php83 bin/test     # before PHP 8.5, leaves out tests/Pipe (|>)
composer lint                   # naming conventions

The test suite compares phplyr with R. The scripts in tests/r/ write the reference fixtures. The lock file is resolved for PHP 8.3 (config.platform), so it installs on every supported version. After adding or changing a table function, run bin/chain-docblock to update the method chain's @method list.

The documentation is a Quarto project in docs/. docs/development.qmd describes the examples, rendering, and the files in site/.

License

MIT. See LICENSE.