as_factor {encode} | R Documentation |
Coerces to factor, blending levels with encoding, if present as a 'guide' attribute. Vectors without encodings (or with empty encodings) acquire levels equal to unique(x)
(notice that storage order controls presentation order). Vectors with non-empty encodings are decoded after harmonizing the encoding and the actual data. Factors with encodings defer to order and display value of the encoding as much as possible. Missing levels are supplied. Unused levels are removed. Other attributes beside 'class' and 'levels' are preserved.
as_factor(x)
x |
vector or factor |
factor
Other decode: decode.data.frame
,
decode.default
, decode
library(magrittr) foo <- c(1, 2, NA, 4, 5) as_factor(foo) as_factor(factor(foo)) as_factor(as.factor(foo)) as_factor(structure(foo, guide = '....')) as_factor(structure(foo, guide = '//5//')) as_factor(structure(foo, guide = '//5/bar//')) as_factor(structure(foo, guide = '//5/bar//6/baz//')) as_factor(structure(factor(foo), guide = '//5/bar//')) as_factor(structure(factor(foo), guide = '//5/bar//')) %>% sort as_factor(structure(factor(foo), guide = '....')) as_factor(structure(factor(foo), guide = '//1/bar//5/bar//'))