Select names matching a pattern

select_grep(DT, patterns, .and = NULL, .but.not = NULL,
  ignore.case = FALSE, perl = TRUE, fixed = FALSE,
  useBytes = FALSE, invert = FALSE, .warn.fixed.mismatch = TRUE)

Arguments

DT

A data.frame.

patterns

Regular expressions to be matched against the names of DT. If length(patterns) > 1 the patterns are concatenated using alternation.

.and

Character or integer positions of names to select, regardless of whether or not they are matched by patterns.

.but.not

Character or integer positions of names to drop, regardless of whether or not they are matched by patterns or whether they are explicitly added by .and.

ignore.case, perl, fixed, useBytes, invert

Arguments passed to grep. Note that perl = TRUE by default (unlike grep) unless fixed = TRUE (and perl is missing).

.warn.fixed.mismatch

(logical, default: TRUE) If TRUE, the default, selecting fixed = TRUE with perl = TRUE or ignore.case = TRUE results in perl and ignore.case being reset to FALSE with a warning (as in grep), even if it makes no difference to the columns eventually selected. If FALSE unambiguous results are allowed; if ignore.case = TRUE and fixed = TRUE, the result is unambiguous if select_grep(DT, tolower(patterns), fixed = TRUE) and select_grep(DT, toupper(patterns), fixed = TRUE) are identical.

Value

DT with the selected names.

integer vector of positions

Examples

library(data.table) dt <- data.table(x1 = 1, x2 = 2, y = 0) select_grep(dt, "x")
#> x1 x2 #> 1: 1 2
select_grep(dt, "x", .and = "y")
#> x1 x2 y #> 1: 1 2 0
select_grep(dt, "x", .and = "y", .but.not = "x2")
#> x1 y #> 1: 1 0