select_helpers {tidyselect} | R Documentation |
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
num_range()
: a numerical range like x01, x02, x03.
one_of()
: variables in character vector.
everything()
: all variables.
last_col()
: last variable, possibly with an offset.
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, vars = peek_vars()) num_range(prefix, range, width = NULL, vars = peek_vars()) one_of(..., .vars = peek_vars()) everything(vars = peek_vars()) last_col(offset = 0L, vars = peek_vars())
match |
A string. |
ignore.case |
If |
vars, .vars |
A character vector of variable names. When called
from inside selecting functions like |
prefix |
A prefix that starts the numeric range. |
range |
A sequence of integers, like |
width |
Optionally, the "width" of the numeric range. For example, a range of 2 gives "01", a range of three "001", etc. |
... |
One or more character vectors. |
offset |
Set it to |
An integer vector giving the position of the matched variables.
nms <- names(iris) vars_select(nms, starts_with("Petal")) vars_select(nms, ends_with("Width")) vars_select(nms, contains("etal")) vars_select(nms, matches(".t.")) vars_select(nms, Petal.Length, Petal.Width) vars_select(nms, everything()) vars_select(nms, last_col()) vars_select(nms, last_col(offset = 2)) vars <- c("Petal.Length", "Petal.Width") vars_select(nms, one_of(vars))