phpstats / phplyr
A dplyr-style grammar of data manipulation for PHP 8.3+, with method chains or PHP 8.5 pipes
Requires
- php: >=8.3 <8.6
- ext-mbstring: *
- phpstats/phibble: ^0.3 || ^0.4 || ^0.5
- symfony/polyfill-php84: ^1.31
Requires (Dev)
- pestphp/pest: ^4.0
- squizlabs/php_codesniffer: ^3.13
Suggests
Provides
None
Conflicts
None
Replaces
None
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:
- Getting started
- Coming from dplyr
- Reference pages for single-table functions, joins, grouping, vector helpers, strings and dates, column selection, tidyr, janitor, models, and databases
- Gallery and Porting an R script
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.