factor2 | R Documentation |
Convenience function to combine level(s) of a vector into a new or existing factor level(s) by unique value and/or regular expression.
Requires R >= 3.5.0
for duplicated labels in factor
s.
For R < 3.5.0
, see combine_levels
.
factor2(
x = character(0L),
levels = NULL,
exclude = NA,
ordered = is.ordered(x),
regex = FALSE,
keep.na = TRUE,
...
)
x |
a vector of data, usually taking a small number of distinct values |
levels |
a named list of unique values or regular expressions
to group elements of a if |
exclude , ordered |
passed to |
regex |
logical; if |
keep.na |
logical; if |
... |
additional arguments passed to |
combine_levels
; combine_regex
## combine numeric, character, or factor
x <- rep(1:5, each = 2)
factor2(x, list('1' = 1:2))
factor2(x, list(a = 1:2, b = 3)) ## all non matches unchanged
factor2(x, list(a = 1:2, b = NULL)) ## all non matches grouped to b
## levels may be swapped without losing original distinction
factor2(x, list('3' = 1:2, '1' = 3))
factor2(x, list('1' = 3, '3' = 1:2))
## combine by regular expression
x <- letters[1:5]
factor2(x, list(a = c('a', 'b')), regex = TRUE)
factor2(x, list(a = c('a|b')), regex = TRUE) ## same
factor2(x, list(a = 'a', b = 'b|c|e'), regex = TRUE)
## use regex only when flagged with (?r)
## note: must use regex = FALSE, otherwise all are treated as regex
x <- c('a', 'aaa', 'abc', 'def')
factor2(x, list(a = 'a', b = '(?r)a.', d = '(?r)d'))
factor2(x, list(a = 'a', b = '(?r)a.', d = 'd'))
## working with NAs
x <- c(1:5, NA)
# factor(x, x, c(1:5, 5), exclude = NA) ## default for factor - error
factor(x, x, c(1:5, 5), exclude = FALSE) ## converts NA
factor2(x, list(x = NULL), exclude = FALSE, keep.na = FALSE) ## converts NA
factor2(x, list(x = NULL), keep.na = TRUE) ## keeps NA
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.