Subset an object by different criteria
Source:R/generics.R
, R/fnames.R
, R/freqlist.R
, and 2 more
brackets.Rd
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
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 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 callingkeep_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
# 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
## like keep_re()
flist[re("[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
flist[re("[ao]"), invert = TRUE]
#> 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
## like keep_pos()
flist[type_freqs(flist) < 2]
#> 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
flist[ranks(flist) <= 3]
#> Frequency list (types in list: 3, tokens in list: 4)
#> <total number of tokens: 5>
#> rank orig_rank type abs_freq nrm_freq
#> ---- --------- ---- -------- --------
#> 1 1 the 2 4000
#> 2 2 and 1 2000
#> 3 3 man 1 2000
flist[ranks(flist) <= 3, invert = TRUE]
#> Frequency list (types in list: 1, tokens in list: 1)
#> <total number of tokens: 5>
#> rank orig_rank type abs_freq nrm_freq
#> ---- --------- ----- -------- --------
#> 1 4 mouse 1 2000
flist[2:3]
#> Frequency list (types in list: 2, tokens in list: 2)
#> <total number of tokens: 5>
#> rank orig_rank type abs_freq nrm_freq
#> ---- --------- ---- -------- --------
#> 1 2 and 1 2000
#> 2 3 man 1 2000
## like keep_bool()
(flist2 <- keep_bool(flist, type_freqs(flist) < 2))
#> 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
flist2[orig_ranks(flist2) > 2]
#> Frequency list (types in list: 2, tokens in list: 2)
#> <total number of tokens: 5>
#> rank orig_rank type abs_freq nrm_freq
#> ---- --------- ----- -------- --------
#> 1 3 man 1 2000
#> 2 4 mouse 1 2000
## like keep_types()
flist[c("man", "and")]
#> Frequency list (types in list: 2, tokens in list: 2)
#> <total number of tokens: 5>
#> rank orig_rank type abs_freq nrm_freq
#> ---- --------- ---- -------- --------
#> 2 3 man 1 2000
#> 1 2 and 1 2000
flist[as_types(c("man", "and"))]
#> Frequency list (types in list: 2, tokens in list: 2)
#> <total number of tokens: 5>
#> rank orig_rank type abs_freq nrm_freq
#> ---- --------- ---- -------- --------
#> 1 2 and 1 2000
#> 2 3 man 1 2000
# 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
tps[c(1, 3, 5, 7, 9)]
#> Type collection of length 5
#> type
#> ----
#> 1 a
#> 2 c
#> 3 e
#> 4 g
#> 5 i
tps[c(TRUE, FALSE)]
#> Type collection of length 5
#> type
#> ----
#> 1 a
#> 2 c
#> 3 e
#> 4 g
#> 5 i
tps[c("a", "c", "e", "g", "i")]
#> Type collection of length 5
#> type
#> ----
#> 1 a
#> 2 c
#> 3 e
#> 4 g
#> 5 i
tps[c(1, 3, 5, 7, 9), invert = TRUE]
#> Type collection of length 5
#> type
#> ----
#> 1 b
#> 2 d
#> 3 f
#> 4 h
#> 5 j
tps[c(TRUE, FALSE), invert = TRUE]
#> Type collection of length 5
#> type
#> ----
#> 1 b
#> 2 d
#> 3 f
#> 4 h
#> 5 j
tps[c("a", "c", "e", "g", "i"), invert = TRUE]
#> 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
tks[re("[acegi]"), invert = TRUE]
#> Token sequence of length 5
#> idx token
#> --- -----
#> 1 b
#> 2 d
#> 3 f
#> 4 h
#> 5 j
tks[c(1, 3, 5, 7, 9), invert = TRUE]
#> Token sequence of length 5
#> idx token
#> --- -----
#> 1 b
#> 2 d
#> 3 f
#> 4 h
#> 5 j
tks[c(TRUE, FALSE), invert = TRUE]
#> Token sequence of length 5
#> idx token
#> --- -----
#> 1 b
#> 2 d
#> 3 f
#> 4 h
#> 5 j
tks[c("a", "c", "e", "g", "i"), invert = TRUE]
#> Token sequence of length 5
#> idx token
#> --- -----
#> 1 b
#> 2 d
#> 3 f
#> 4 h
#> 5 j