phibble

Types and conversion

Column types, casts, factors, times of day, and conversion to model data.

Column types

->cast()

$table->cast(array $types): DataFrame

Convert columns to other types.

Parameter Type Default Description
$types array<string, Type|string>

Method of DataFrame
Returns DataFrame
Examples Types

Type::parse()

Type::parse(Type|string $type): Type

A Type from a case or its name, such as ‘float’ or ‘date’, in any letter case.

Parameter Type Default Description
$type Type|string

Static method of Phibble\Type
Returns Type
Throws InvalidArgumentException: for an unknown type name.

Type->abbreviation()

$type->abbreviation(): string

The short label printed in a column header, as in tibble: dbl, int, lgl, chr, fct, date, dttm, time, or list.

Method of Phibble\Type
Returns string

Type->isNumeric()

$type->isNumeric(): bool

Whether the type is numeric: float, int, or bool.

Method of Phibble\Type
Returns bool

Factors

->asFactor()

$table->asFactor(    string $name,    ?array $levels = null,    bool $ordered = false,): DataFrame

Turn a column into a factor, as R’s factor(). Without levels, they are the sorted unique values. Values not in $levels become NA.

Parameter Type Default Description
$name string
$levels list<string|int|float>|null null
$ordered bool false

Method of DataFrame
Returns DataFrame
Examples Types, Getting started

->asOrdered()

$table->asOrdered(string $name, ?array $levels = null): DataFrame

Convert a column to an ordered factor, as R’s factor(ordered = TRUE).

An existing factor keeps its levels unless $levels is given.

Parameter Type Default Description
$name string
$levels list<string|int|float>|null null Levels in order; null takes the sorted distinct values.

Method of DataFrame
Returns DataFrame
Examples Types

->isOrdered()

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

Whether the column is an ordered factor.

Parameter Type Default Description
$name string

Method of DataFrame
Returns bool
Examples Types

->levels()

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

The levels of a factor column, in level order.

Parameter Type Default Description
$name string

Method of DataFrame
Returns list<string>
Examples Types

->fctLevels()

$table->fctLevels(string $name, array $levels): DataFrame

factor(x, levels = ...): set the full level order. Unlisted values become NA.

Parameter Type Default Description
$name string
$levels array

Method of DataFrame
Returns DataFrame

->fctRelevel()

$table->fctRelevel(    string $name,    array $levels,    int $after = 0,): DataFrame

Move levels to the front, or after a 0-based position (-1 for the end), as forcats’ fct_relevel().

Parameter Type Default Description
$name string
$levels array
$after int 0

Method of DataFrame
Returns DataFrame
Examples Types

->fctRev()

$table->fctRev(string $name): DataFrame

fct_rev()

Parameter Type Default Description
$name string

Method of DataFrame
Returns DataFrame
Examples Types

->fctInorder()

$table->fctInorder(string $name): DataFrame

fct_inorder()

Parameter Type Default Description
$name string

Method of DataFrame
Returns DataFrame

->fctInfreq()

$table->fctInfreq(string $name): DataFrame

fct_infreq()

Parameter Type Default Description
$name string

Method of DataFrame
Returns DataFrame
Examples Types

->fctReorder()

$table->fctReorder(    string $name,    string $by,    ?Closure $summary = null,    bool $descending = false,): DataFrame

Order levels by a summary (default median) of another column, as forcats’ fct_reorder().

Parameter Type Default Description
$name string
$by string
$summary Closure(list<mixed>): mixed|null null
$descending bool false

Method of DataFrame
Returns DataFrame
Examples Types

->fctRecode()

$table->fctRecode(string $name, array $map): DataFrame

Rename levels, new => old, as forcats’ fct_recode().

Parameter Type Default Description
$name string
$map array

Method of DataFrame
Returns DataFrame
Examples Types

->fctCollapse()

$table->fctCollapse(string $name, array $groups): DataFrame

Merge levels, new => list of old, as forcats’ fct_collapse().

Parameter Type Default Description
$name string
$groups array

Method of DataFrame
Returns DataFrame
Examples Types

->fctDrop()

$table->fctDrop(?string $name = null, ?array $only = null): DataFrame

fct_drop() / droplevels(): remove unused levels. With no name, every factor column.

Parameter Type Default Description
$name ?string null
$only list<string>|null null

Method of DataFrame
Returns DataFrame

Times of day

Time::of()

Time::of(int $hours, int $minutes = 0, int|float $seconds = 0): Time

A time from hours, minutes, and seconds, as hms::hms().

Parameter Type Default Description
$hours int
$minutes int 0
$seconds int|float 0

Static method of Phibble\Time
Returns Time
Examples Types

Time::fromSeconds()

Time::fromSeconds(int|float $seconds): Time

A time from a number of seconds, rounded to the microsecond.

Parameter Type Default Description
$seconds int|float

Static method of Phibble\Time
Returns Time
Examples Types

Time::parse()

Time::parse(string $text): ?Time

Text such as “07:30”, “7:30:15.25”, “-01:00:00”, “100:00:00”, or “1:30 PM” as a time, or null when it is not one. Unlike reading CSV, which follows readr, fractional seconds are kept.

Parameter Type Default Description
$text string

Static method of Phibble\Time
Returns ?Time
Examples Types

Time::meridiem()

Time::meridiem(int $hours, string $suffix): int

readr’s AM/PM rule: 12 AM is hour 0, and PM adds 12 hours to any hour but 12.

Parameter Type Default Description
$hours int
$suffix string

Static method of Phibble\Time
Returns int

Time->seconds()

$time->seconds(): float

The number of seconds, as R’s as.numeric() of an hms.

Method of Phibble\Time
Returns float
Examples Types

Time->hour()

$time->hour(): int

The hour of the day, 0 to 23, as lubridate::hour().

Method of Phibble\Time
Returns int

Time->minute()

$time->minute(): int

The minute of the hour, as lubridate::minute().

Method of Phibble\Time
Returns int

Time->second()

$time->second(): float

The second of the minute, with its fraction, as lubridate::second().

Method of Phibble\Time
Returns float

Model data

->toModelData()

$table->toModelData(?array $names = null): array

Columns for model fitting with formulas (Phit\fit_ols, Phit\fit_generalized_linear, Phit\model_matrix):

  • float, int: list<float|int|null>
  • bool: list<bool|null> (a two-level contrast, as in R)
  • string: list<string|null> (the model sorts levels, as R does)
  • date: days since 1970-01-01; datetime: seconds since epoch
  • factor: [‘levels’ => list, ‘codes’ => list, ‘ordered’ => bool], with 0-based codes, null for NA, and every level in table order

List-columns cannot be modelled and throw.

Parameter Type Default Description
$names list<string>|null null Columns to include; all by default.

Method of DataFrame
Returns array<string, list<mixed>|array{levels: list<string>, codes: list<?int>, ordered: bool}>
Examples Types

->toMatrix()

$table->toMatrix(array $names): array

Rows of numeric columns as floats, the shape phit’s fit_* functions take for x. Bool columns become 0.0/1.0; NA stays null.

Parameter Type Default Description
$names list<string>

Method of DataFrame
Returns list<list<float|null>>

->toVector()

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

A numeric column as floats, the shape phit’s fit_* functions take for y.

Parameter Type Default Description
$name string

Method of DataFrame
Returns list<float|null>