select_helpers: Select Helpers

Description Usage Arguments Value See Also Examples

Description

These functions allow you to select variables based on their names.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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

character(n). If length > 1, the union of the matches is taken.

ignore.case

logical(1). If TRUE, the default, ignores case when matching names.

vars

character(n). A character vector of variable names. When called from inside selecting functions such as select(), these are automatically set to the names of the table.

perl

logical(1). Should Perl-compatible regexps be used?

prefix

A prefix which starts the numeric range.

range

integer(n). A sequence of integers, e.g. 1:5.

width

numeric(1). Optionally, the "width" of the numeric range. For example, a range of 2 gives "01", a range of three "001", etc.

x

character(n). A vector of column names.

offset

integer(1). Select the nth variable from the end of the data.frame.

Value

An integer vector giving the position of the matched variables.

See Also

select(), relocate(), where(), group_cols()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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")))

poorman documentation built on March 28, 2021, 5:05 p.m.