Guides
Coming from tibble
phibble follows tibble, readr, and forcats closely. Function names become camelCase methods on DataFrame, R’s named arguments become PHP named arguments, and NULL/NA become PHP null. The tables below map the R functions to their phibble equivalents.
tibble
| R | phibble |
|---|---|
tibble(x = 1:3, y = x * 2) |
tibble(x: [1, 2, 3], y: fn ($t) => ...) |
tribble(~x, ~y, 1, "a") |
tribble(['x', 'y'], [1, 'a']) or tribble(['x', 'y'], 1, 'a') |
as_tibble(x) |
as_tibble($x) or DataFrame::asTibble($x) |
add_row(df, x = 1, .before = 2) |
$df->addRow(['x' => 1], before: 1) |
add_column(df, z = 0, .after = "x") |
$df->addColumn(['z' => 0], after: 'x') |
enframe(x), deframe(df) |
enframe($x), $df->deframe() |
glimpse(df) |
echo $df->glimpse() |
print(df, n = 30) |
echo $df->format(n: 30) |
df$x, df[["x"]] |
$df->x, $df['x'] |
nrow(df), ncol(df), names(df) |
$df->nrow(), $df->ncol(), $df->columns() |
.name_repair = "unique" |
nameRepair: 'unique' (also check_unique, universal, minimal) |
In a tibble() call, a closure receives the data frame built so far and returns a list or a single value. Only single values are recycled, and incompatible lengths throw with tibble’s message.
readr
| R | phibble |
|---|---|
read_csv(file) |
DataFrame::fromCsv($file) |
read_delim(file, delim = ";") |
DataFrame::fromCsv($file, delimiter: ';') |
read_csv(I("a,b\n1,2")) |
DataFrame::fromCsvString("a,b\n1,2") |
col_types = "icd" |
types: 'icd' |
col_types = cols(x = col_factor()) |
types: ['x' => 'factor'] |
col_select, col_names, skip, n_max |
select:, colNames:, skip:, nMax: |
na, comment, trim_ws, guess_max |
na:, comment:, trimWs:, guessMax: |
locale(decimal_mark = ",") |
decimalMark: ',', groupingMark: '.' |
write_csv(df, file) |
$df->writeCsv($file) |
format_csv(df) |
$df->toCsv() |
forcats
Factor functions are methods that take the column name first.
| R | phibble |
|---|---|
factor(x), factor(x, levels = l) |
$df->asFactor('x'), $df->asFactor('x', $l) |
ordered(x, levels = l) |
$df->asOrdered('x', $l) |
levels(x) |
$df->levels('x') |
fct_relevel(x, "b") |
$df->fctRelevel('x', ['b']) |
fct_rev, fct_inorder, fct_infreq |
fctRev, fctInorder, fctInfreq |
fct_reorder(x, y) |
$df->fctReorder('x', 'y') |
fct_recode(x, new = "old") |
$df->fctRecode('x', ['new' => 'old']) |
fct_collapse(x, ab = c("a", "b")) |
$df->fctCollapse('x', ['ab' => ['a', 'b']]) |
fct_drop(x), droplevels(df) |
$df->fctDrop('x'), $df->fctDrop() |
dplyr and tidyr basics
phibble’s methods cover the verbs that other backends need to implement natively. phplyr provides the full dplyr and tidyr interface as pipe-able functions.
| R | phibble |
|---|---|
filter, mutate, select, rename, arrange |
methods of the same names |
rename(df, new = old) |
$df->rename(['new' => 'old']) |
group_by, ungroup, rowwise |
groupBy, ungroup, rowwise |
summarise(.groups = "drop") |
$df->summarize([...], 'drop') or $df->summarise([...], 'drop') |
left_join(x, y, by = "k") |
$x->join($y, ['k'], 'left') |
left_join(x, y, by = c("a" = "b")) |
$x->join($y, [['a', 'b']], 'left') |
bind_rows, bind_cols |
DataFrame::concat([...]), DataFrame::bindCols([...]) |
nest(df, data = c(x, y)), unnest(df, data) |
$df->nest(['x', 'y']), $df->unnest('data') |
pivot_longer_spec(df, spec), pivot_wider_spec(df, spec) |
$df->pivotLongerSpec($spec), $df->pivotWiderSpec($spec); phplyr’s pivot_longer() and pivot_wider() build the spec |
drop_na(df, x) |
$df->dropNa(['x']) |
Differences
phibble differs from R in these places:
- Whole numbers guess as int. readr guesses
1,2,3as double. phibble guesses int, which suits PHP. Passtypes: 'd'to read a double. - Positions are 0-based.
addRow(before:),addColumn(before:),take(),slice(), androw()count rows and columns from 0. - Regular expressions are PCRE. As in stringr, a text pattern is a regular expression and
fixed()matches literally. phibble uses PCRE where stringr uses ICU.SqlTableruns most regular-expression patterns in memory, as the SQL tables page lists. - There is no time-of-day type. readr’s
hmscolumns and thetcolumn type stay strings. - JSON keeps null values. jsonlite drops NA fields from records.
toJson()writes them asnull. - Datetimes are UTC. Text without an offset is read as UTC, and text with an offset is converted to UTC, as readr does.
- Strings sort in byte order. Sorting, grouping, and factor levels use the C locale, as dplyr’s
arrange()does. Base R’sfactor()uses the system locale, which can order mixed-case levels differently. - Packed data frame columns are not supported. An unnamed table in
tibble()is spliced into columns. A named one throws.
From phpstats/phibble@36d0f3f.