as_factor: Coerce to Factor using Encoding if Present

Description Usage Arguments Value Examples

View source: R/utils.R

Description

Coerces to factor, blending levels with encoding, if present. 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

1

Arguments

x

vector or factor

Value

factor

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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//'))

metaplot documentation built on Sept. 30, 2018, 5:05 p.m.