Low-level encoding and factor building
Arguments
- x
A vector of values
- from, to
Vectors of the same length. Values in
xare matched tofromand replaced with the corresponding value into.- strict
If
TRUE, values inxthat are not matched tofromwill be replaced withNA. IfFALSE, they will be left unchanged.- exclude
Values in
xthat will not be matched when recoding;excludewill take priority overfrominencode().- levels
A vector of unique values. If
NULL, the unique values inxare used.
Value
encode() A vector of the same length as x with values
replaced according to from and to.
fact() A factor vector with levels corresponding to the
unique values in x (or levels if provided).
Examples
fact(strsplit("factor function", "")[[1L]])
#> [1] f a c t o r f u n c t i o n
#> Levels: f a c t o r u n i
# encode() can be used to for the same utility as factor(x, levels, labels)
en <- encode(
strsplit("jordan", "")[[1L]],
from = c("a", "o"),
to = "*",
)
en
#> [1] "j" "*" "r" "d" "*" "n"
fact(en)
#> [1] j * r d * n
#> Levels: j * r d n
