phibble

Rows and columns

Methods that read, select, filter, sort, and add rows and columns.

Size and names

->columns()

$table->columns(): array

Column names in order.

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

->types()

$table->types(): array

Column types by name.

Method of DataFrame, SqlTable, PolarsTable
Returns array<string, Type>

->type()

$table->type(string $name): Type

One column’s type.

Parameter Type Default Description
$name string

Method of DataFrame
Returns Type
Throws ColumnNotFound: when the column does not exist.

->nrow()

$table->nrow(): int

Number of rows. SqlTable runs a COUNT(*) query.

Method of DataFrame, SqlTable, PolarsTable
Returns int
Examples Types

->ncol()

$table->ncol(): int

Number of columns.

Method of DataFrame, SqlTable, PolarsTable
Returns int

->count()

$table->count(): int

Number of rows, for count($df).

Method of DataFrame, SqlTable, PolarsTable
Returns int

->has()

$table->has(string $name): bool

Whether the column exists.

Parameter Type Default Description
$name string

Method of DataFrame
Returns bool

Values

->column()

$table->column(string $name): array

One column’s values as a PHP list. Factors return their labels.

Parameter Type Default Description
$name string

Method of DataFrame, SqlTable, PolarsTable
Returns list<mixed>
Throws ColumnNotFound: when the column does not exist.
Examples Getting started

->row()

$table->row(int $index): Row

One row by 0-based position.

Parameter Type Default Description
$index int

Method of DataFrame
Returns Row
Throws InvalidArgumentException: when the position is out of range.
Examples Getting started

Row->get()

$row->get(string $name): mixed

One value by column name. $row->name and $row['name'] read the same value.

Parameter Type Default Description
$name string

Method of Phibble\Row
Returns mixed
Throws ColumnNotFound: when the column does not exist.
Examples phibble for Laravel developers

Row->has()

$row->has(string $name): bool

Whether the row has the column.

Parameter Type Default Description
$name string

Method of Phibble\Row
Returns bool

Row->toArray()

$row->toArray(): array

The row as an associative array of column name => value.

Method of Phibble\Row
Returns array<string, mixed>
Examples Expressions

Selecting and renaming

->select()

$table->select(array $names): Table

Keep the named columns, in the given order.

Parameter Type Default Description
$names list<string> Column names.

Method of DataFrame, SqlTable, PolarsTable
Returns Table
Throws ColumnNotFound: when a column does not exist.
Examples Expressions, Getting started, phibble for Laravel developers

->rename()

$table->rename(array $map): Table

Rename columns.

Parameter Type Default Description
$map array<string, string> New name => old name, as in dplyr.

Method of DataFrame, SqlTable, PolarsTable
Returns Table
Throws ColumnNotFound: when an old name does not exist.
Examples Getting started

NameRepair::repair()

NameRepair::repair(    array $names,    Closure|string $mode = 'check_unique',): array

Repair column names, as vctrs::vec_as_names().

Modes: minimal makes names non-null; unique adds a …n suffix to empty, ..., ..j, and duplicated names; check_unique throws unless names are unique and non-empty; universal is unique and also makes names syntactic in R. A closure receives the names and returns the repaired list.

Parameter Type Default Description
$names list<string|null>
$mode string|Closure(list<string>): list<string> ‘check_unique’ minimal, unique, check_unique, universal, or a closure.

Static method of Phibble\NameRepair
Returns list<string>
Throws InvalidArgumentException: when check_unique finds empty or duplicated names, or a closure returns a different number of names.

Filtering and sorting

->filter()

$table->filter(Closure|Expr $predicate): Table

Keep rows where the predicate is true. NA counts as false.

A closure runs once per row. An Expr runs once per group, a whole column at a time, so aggregates such as mean() compare within the group.

Parameter Type Default Description
$predicate Closure(Row, Table): ?bool|Expr A row closure or an expression.

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

->arrange()

$table->arrange(array $keys): Table

Sort rows. The sort is stable, and NA sorts last in both directions.

Parameter Type Default Description
$keys list<string|array{0: string, 1?: ‘asc’|‘desc’}|Expr> A column name sorts ascending; [name, ‘desc’] sorts descending; an Expr sorts by its value, and desc() reverses it.

Method of DataFrame, SqlTable, PolarsTable
Returns Table
Examples Getting started, Types, phibble for Laravel developers

->distinct()

$table->distinct(array $names = []): Table

Unique rows over the named columns, keeping the first of each and only those columns.

Parameter Type Default Description
$names list<string> [] Columns to compare. An empty list compares all columns.

Method of DataFrame, SqlTable, PolarsTable
Returns Table
Examples Getting started

->dropNa()

$table->dropNa(array $names = []): DataFrame

Drop rows with NA in any of the named columns (all columns when empty).

Parameter Type Default Description
$names list<string> []

Method of DataFrame
Returns DataFrame
Examples Getting started

Adding columns and rows

->mutate()

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

Add or replace columns.

Each value is a closure run per row, an Expr evaluated per group (aggregates repeat within the group), a list with one value per row, or a scalar to repeat. Later entries can use earlier ones.

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

Method of DataFrame, SqlTable, PolarsTable
Returns Table
Examples Expressions, Getting started, Types

->withColumn()

$table->withColumn(    string $name,    Column\Column|array $values,    Type|string|null $type = null,): DataFrame

Add or replace one column. Replaced columns keep their position.

Parameter Type Default Description
$name string
$values list<mixed>|Column
$type Type|string|null null

Method of DataFrame
Returns DataFrame

->addColumn()

$table->addColumn(    array $columns,    string|int|null $before = null,    string|int|null $after = null,): DataFrame

Insert columns, as tibble’s add_column(), with tibble()’s recycling rules. Place them before or after a column, by name or 0-based position; by default at the end.

Parameter Type Default Description
$columns array<string, mixed>
$before string|int|null null
$after string|int|null null

Method of DataFrame
Returns DataFrame

->addRow()

$table->addRow(    array $rows = [],    ?int $before = null,    ?int $after = null,): DataFrame

Insert rows, as tibble’s add_row(). Pass one row (name => value), a list of rows, or nothing for an all-NA row. Missing columns are NA; unknown columns throw. Positions are 0-based: $before inserts before that row, $after after it. The default appends.

Parameter Type Default Description
$rows array<string, mixed>|list<array<string, mixed>> []
$before ?int null
$after ?int null

Method of DataFrame
Returns DataFrame

Rows by position

->slice()

$table->slice(int $offset, ?int $length = null): Table

Rows from $offset, $length of them or to the end. Ignores groups.

Parameter Type Default Description
$offset int 0-based start; negative counts from the end.
$length int|null null Number of rows; null for the rest, negative to stop that many rows before the end.

Method of DataFrame, SqlTable, PolarsTable
Returns Table

->tail()

$table->tail(int $n = 6): DataFrame

The last n rows; a negative n drops the first |n| rows, as in R.

Parameter Type Default Description
$n int 6

Method of DataFrame
Returns DataFrame

->take()

$table->take(array $indices): Table

Rows by 0-based position, in the given order. A null index gives an all-NA row.

Types, factor levels, and grouping are kept.

Parameter Type Default Description
$indices list<int|null>

Method of DataFrame, SqlTable, PolarsTable
Returns Table