select_helpers {poorman} | R Documentation |
Select Helpers
Description
These functions allow you to select variables based on their names.
-
starts_with()
: Starts with a prefix. -
ends_with()
: Ends with a prefix. -
contains()
: Contains a literal string. -
matches()
: Matches a regular expression. -
all_of()
: Matches variable names in a character vector. All names must be present, otherwise an error is thrown. -
any_of()
: The same asall_of()
except it doesn't throw an error. -
everything()
: Matches all variables. -
last_col()
: Select the last variable, possibly with an offset.
Usage
starts_with(match, ignore.case = TRUE, vars = peek_vars())
ends_with(match, ignore.case = TRUE, vars = peek_vars())
contains(match, ignore.case = TRUE, vars = peek_vars())
matches(match, ignore.case = TRUE, perl = FALSE, vars = peek_vars())
num_range(prefix, range, width = NULL, vars = peek_vars())
all_of(x, vars = peek_vars())
any_of(x, vars = peek_vars())
everything(vars = peek_vars())
last_col(offset = 0L, vars = peek_vars())
Arguments
match |
|
ignore.case |
|
vars |
|
perl |
|
prefix |
A prefix which starts the numeric range. |
range |
|
width |
|
x |
|
offset |
|
Value
An integer vector giving the position of the matched variables.
See Also
select()
, relocate()
, where()
, group_cols()
Examples
mtcars %>% select(starts_with("c"))
mtcars %>% select(starts_with(c("c", "h")))
mtcars %>% select(ends_with("b"))
mtcars %>% relocate(contains("a"), .before = mpg)
iris %>% select(matches(".t."))
mtcars %>% select(last_col())
# `all_of()` selects the variables in a character vector:
iris %>% select(all_of(c("Petal.Length", "Petal.Width")))
# `all_of()` is strict and will throw an error if the column name isn't found
try({iris %>% select(all_of(c("Species", "Genres")))})
# However `any_of()` allows missing variables
iris %>% select(any_of(c("Species", "Genres")))