Learn R Programming

encode (version 0.3.6)

as_factor: Coerce to Factor using Encoding if Present

Description

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.

Usage

as_factor(x)

Arguments

x

vector or factor

Value

factor

See Also

Other decode: decode.data.frame, decode.default, decode

Examples

Run this code
# NOT RUN {
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//'))


# }

Run the code above in your browser using DataLab