Reference
tidyselect and across()
Selectors
Selectors pick columns by name, pattern, or type. They work in select(), relocate(), rename_with(), arrange(), distinct(), group_by(), count(), across(), pick(), and the tidyr functions.
| Selector | Columns |
|---|---|
'x', ['x', 'y'] |
By name |
starts_with('x'), ends_with(), contains() |
By name fragment; case-insensitive unless ignoreCase: false |
matches('^x\\d+$') |
By regular expression |
num_range('x', 1, 3) |
x1, x2, x3 |
everything(), last_col() |
All columns, the last column |
all_of([...]), any_of([...]) |
Names from a list, strict or lenient |
where('numeric'), where(fn ($values, $type, $name) => ...) |
By type or predicate |
group_cols() |
The grouping columns |
except(...) |
Everything but these (dplyr’s -x) |
any_selected(...), all_selected(...) |
Union and intersection |
where() accepts 'numeric', 'integer', 'double', 'character', 'logical', 'factor', 'date', 'datetime', and 'list', or a Phibble\Type. 'numeric' selects integer and double columns.
across()
across(cols, fns, names:) applies functions to several columns inside mutate() or summarize(). Grouping columns are never selected. fns is one function or an associative array of named functions. Output names default to {col} for one function and {col}_{fn} for several.
$schools = DataFrame::fromCsv('schools.csv') |> group_by('school');
// Phplyr helpers can be passed by name; each gets the column name.
echo $schools |>
summarize(across(where('numeric'), ['avg' => mean(...), 'sd' => sd(...)])) |>
head(3);
// Other closures get the column's values (summarize) or each value (mutate).
echo "\n", $schools |>
summarize(across(['study_hours', 'score'], fn (array $v) => round(\max($v) - \min($v), 1), names: 'range_{col}')) |>
head(3);
echo "\n", $schools |> ungroup() |> mutate(across(ends_with('score'), fn ($v) => round($v))) |> head(3);$schools = phplyr(DataFrame::fromCsv('schools.csv'))->groupBy('school')->get();
// Phplyr helpers can be passed by name; each gets the column name.
echo phplyr($schools)
->summarize(across(where('numeric'), ['avg' => mean(...), 'sd' => sd(...)]))
->head(3);
// Other closures get the column's values (summarize) or each value (mutate).
echo "\n", phplyr($schools)
->summarize(across(['study_hours', 'score'], fn (array $v) => round(\max($v) - \min($v), 1), names: 'range_{col}'))
->head(3);
echo "\n", phplyr($schools)->ungroup()->mutate(across(ends_with('score'), fn ($v) => round($v)))->head(3);# A tibble: 3 × 7
school study_hours_avg study_hours_sd prior_score_avg prior_score_sd score_avg
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 schoo… 6.60 3.26 63.0 12.3 72.9
2 schoo… 6.20 3.57 59.3 13.3 72.9
3 schoo… 6.52 3.34 56.1 11.3 69.0
# ℹ 1 more variable: score_sd <dbl>
# A tibble: 3 × 3
school range_study_hours range_score
<chr> <dbl> <dbl>
1 school-01 10.3 30.9
2 school-02 10.8 34.4
3 school-03 10.3 35.7
# A tibble: 3 × 4
school study_hours prior_score score
<chr> <dbl> <dbl> <dbl>
1 school-01 11.6 77 78
2 school-01 2.7 52 67
3 school-01 10.0 79 88
What each function receives:
- A phplyr helper passed as a first-class callable, such as
mean(...), is called with the column name. - In
summarize(), other closures receive the column’s values as a list. - In
mutate(), other closures receive one value at a time, or the whole column if the first parameter is typedarray. - A closure may return a helper’s result, as in
fn ($v) => mean($v, naRm: true), which is then evaluated on the group.
Inside any of these functions, cur_column() returns the name of the column being processed.
if_any(cols, fn) and if_all(cols, fn) test a predicate across columns, in filter() or as a logical column in mutate(). They use R’s NA logic, so a predicate may return null for NA. With no columns selected, if_any() is false and if_all() is true, as in dplyr 1.1. pick(...) returns the selected columns of the current group as a table, for use inside closures.
From phpstats/phplyr@d319dc5.