Learn R Programming

mclm (version 0.2.7)

brackets: Subset an object by different criteria

Description

This method can be used to subset objects based on different criteria.

Usage

# S3 method for fnames
[(x, i, invert = FALSE, ...)

# S3 method for fnames [(x, i, invert = FALSE) <- value

# S3 method for freqlist [(x, i, invert = FALSE, ...)

# S3 method for tokens [(x, i, invert = FALSE, ...)

# S3 method for tokens [(x, i, invert = FALSE, ...) <- value

# S3 method for types [(x, i, invert = FALSE, ...)

# S3 method for types [(x, i, invert = FALSE) <- value

Value

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

Arguments

x

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

i

Selection criterion; depending on its class, it behaves differently.

invert

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

...

Additional arguments.

value

Value to assign.

Details

The subsetting method with the notation [], applied to mclm objects, is part of a family of subsetting methods: see keep_pos(), keep_re(), keep_types() and keep_bool(). In this case, the argument i is the selection criterion and, depending on its class, the method behaves different:

  • providing a re object is equivalent to calling keep_re(),

  • providing a numeric vector is equivalent to calling keep_pos(),

  • providing a logical vector is equivalent to calling keep_bool(),

  • providing a types object or a character vector is equivalent to calling keep_types().

When the notation x[i, ...] is used, it is also possible to set the invert argument to TRUE (which then is one of the additional arguments in ...). This invert argument then serves the same purpose as the invert argument in the keep_ methods, turning it into a drop_ method.

See Also

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

Examples

Run this code
# For a 'freqlist' object --------------------
(flist <- freqlist("The man and the mouse.", as_text = TRUE))

## like keep_re()
flist[re("[ao]")]
flist[re("[ao]"), invert = TRUE]

## like keep_pos()
flist[type_freqs(flist) < 2]
flist[ranks(flist) <= 3]
flist[ranks(flist) <= 3, invert = TRUE]
flist[2:3]

## like keep_bool()
(flist2 <- keep_bool(flist, type_freqs(flist) < 2))
flist2[orig_ranks(flist2) > 2]

## like keep_types()
flist[c("man", "and")]
flist[as_types(c("man", "and"))]

# For a 'types' object -----------------------
(tps <- as_types(letters[1:10]))

tps[c(1, 3, 5, 7, 9)]
tps[c(TRUE, FALSE)]
tps[c("a", "c", "e", "g", "i")]

tps[c(1, 3, 5, 7, 9), invert = TRUE]
tps[c(TRUE, FALSE), invert = TRUE]
tps[c("a", "c", "e", "g", "i"), invert = TRUE]

# For a 'tokens' object ----------------------
(tks <- as_tokens(letters[1:10]))

tks[re("[acegi]"), invert = TRUE]
tks[c(1, 3, 5, 7, 9), invert = TRUE]
tks[c(TRUE, FALSE), invert = TRUE]
tks[c("a", "c", "e", "g", "i"), invert = TRUE]

Run the code above in your browser using DataLab