Obtain matching indices corresponding to patterns
patterns.Rd
patterns
returns the elements of cols
that match the regular expression patterns, which must be
supported by grep
.
From v1.9.6
, melt.data.table
has an enhanced functionality
in which measure.vars
argument can accept a list of column names
and melt them into separate columns. See the Efficient reshaping using
data.tables
vignette linked below to learn more.
Usage
patterns(
..., cols=character(0),
ignore.case=FALSE, perl=FALSE,
fixed=FALSE, useBytes=FALSE)
Arguments
- ...
A set of regular expression patterns.
- cols
A character vector of names to which each pattern is matched.
- ignore.case, perl, fixed, useBytes
Passed to
grep
.
Examples
DT = data.table(x1 = 1:5, x2 = 6:10, y1 = letters[1:5], y2 = letters[6:10])
# melt all columns that begin with 'x' & 'y', respectively, into separate columns
melt(DT, measure.vars = patterns("^x", "^y", cols=names(DT)))
#> variable value1 value2
#> <fctr> <int> <char>
#> 1: 1 1 a
#> 2: 1 2 b
#> 3: 1 3 c
#> 4: 1 4 d
#> 5: 1 5 e
#> 6: 2 6 f
#> 7: 2 7 g
#> 8: 2 8 h
#> 9: 2 9 i
#> 10: 2 10 j
# when used with melt, 'cols' is implicitly assumed to be names of input
# data.table, if not provided.
melt(DT, measure.vars = patterns("^x", "^y"))
#> variable value1 value2
#> <fctr> <int> <char>
#> 1: 1 1 a
#> 2: 1 2 b
#> 3: 1 3 c
#> 4: 1 4 d
#> 5: 1 5 e
#> 6: 2 6 f
#> 7: 2 7 g
#> 8: 2 8 h
#> 9: 2 9 i
#> 10: 2 10 j