Skip to contents

These methods can be used to subset objects based on a regular expression.

Usage

keep_re(x, pattern, perl = TRUE, invert = FALSE, ...)

drop_re(x, pattern, perl = TRUE, ...)

# S3 method for fnames
drop_re(x, pattern, perl = TRUE, ...)

# S3 method for fnames
keep_re(x, pattern, perl = TRUE, invert = FALSE, ...)

# S3 method for freqlist
drop_re(x, pattern, perl = TRUE, ...)

# S3 method for freqlist
keep_re(x, pattern, perl = TRUE, invert = FALSE, ...)

# S3 method for tokens
drop_re(x, pattern, perl = TRUE, ...)

# S3 method for tokens
keep_re(x, pattern, perl = TRUE, invert = FALSE, ...)

# S3 method for types
drop_re(x, pattern, perl = TRUE, ...)

# S3 method for types
keep_re(x, pattern, perl = TRUE, invert = FALSE, ...)

Arguments

x

An object of any of the classes for which the method is implemented.

pattern

Either an object of the class re or a character vector of length one containing a regular expression.

perl

Logical. Whether pattern is treated as a PCRE flavor regular expression. The perl argument is only used if pattern is a regular character vector. If pattern is an object of the class re, then the perl argument is ignored, and the relevant information in the re object pattern, viz. the value of pattern$perl, is used instead.

invert

Logical. Whether the matches should be selected rather than the non-matches.

...

Additional arguments.

Value

Object of the same class as x with the selected elements only.

Details

The methods keep_pos() and drop_pos() are part of a family of methods of the mclm package used to subset different objects. The methods starting with keep_ extract the items in x based on the criterion specified by the second argument. In contrast, the methods starting with drop_ exclude the items that match the criterion in the same argument.

Calling a drop_ method is equivalent to calling its keep_ counterpart when the invert argument is TRUE.

See also

Other subsetters: brackets, keep_bool(), keep_pos(), keep_types()

Examples

# For a 'freqlist' object --------------------
(flist <- freqlist("The man and the mouse.", as_text = TRUE))
#> Frequency list (types in list: 4, tokens in list: 5)
#> rank  type abs_freq nrm_freq
#> ---- ----- -------- --------
#>    1   the        2     4000
#>    2   and        1     2000
#>    3   man        1     2000
#>    4 mouse        1     2000

keep_re(flist, "[ao]")
#> Frequency list (types in list: 3, tokens in list: 3)
#> <total number of tokens: 5>
#> rank orig_rank  type abs_freq nrm_freq
#> ---- --------- ----- -------- --------
#>    1         2   and        1     2000
#>    2         3   man        1     2000
#>    3         4 mouse        1     2000
drop_re(flist, "[ao]")
#> Frequency list (types in list: 1, tokens in list: 2)
#> <total number of tokens: 5>
#> rank orig_rank type abs_freq nrm_freq
#> ---- --------- ---- -------- --------
#>    1         1  the        2     4000
keep_re(flist, "[ao]", invert = TRUE) # same as drop_re()
#> Frequency list (types in list: 1, tokens in list: 2)
#> <total number of tokens: 5>
#> rank orig_rank type abs_freq nrm_freq
#> ---- --------- ---- -------- --------
#>    1         1  the        2     4000

# For a 'types' object -----------------------
(tps <- as_types(letters[1:10]))
#> Type collection of length 10
#>    type
#>    ----
#>  1    a
#>  2    b
#>  3    c
#>  4    d
#>  5    e
#>  6    f
#>  7    g
#>  8    h
#>  9    i
#> 10    j

keep_re(tps, "[acegi]")
#> Type collection of length 5
#>   type
#>   ----
#> 1    a
#> 2    c
#> 3    e
#> 4    g
#> 5    i
drop_re(tps, "[acegi]")
#> Type collection of length 5
#>   type
#>   ----
#> 1    b
#> 2    d
#> 3    f
#> 4    h
#> 5    j

# For a 'tokens' object ----------------------
(tks <- as_tokens(letters[1:10]))
#> Token sequence of length 10
#> idx token
#> --- -----
#>   1     a
#>   2     b
#>   3     c
#>   4     d
#>   5     e
#>   6     f
#>   7     g
#>   8     h
#>   9     i
#>  10     j

keep_re(tks, "[acegi]")
#> Token sequence of length 5
#> idx token
#> --- -----
#>   1     a
#>   2     c
#>   3     e
#>   4     g
#>   5     i
drop_re(tks, "[acegi]")
#> Token sequence of length 5
#> idx token
#> --- -----
#>   1     b
#>   2     d
#>   3     f
#>   4     h
#>   5     j