Skip to contents

Converts x to a factor and keeps its attributes. Warns appropriately such that the user can decide whether they prefer converting to factor manually (e.g. for full control of factor levels).

Usage

as_factor_keep_attributes(
  x,
  x_name = deparse(substitute(x)),
  na_level = "<Missing>",
  verbose = TRUE
)

Arguments

x

(atomic)
object to convert.

x_name

(string)
name of x.

na_level

(string)
the explicit missing level which should be used when converting a character vector.

verbose

defaults to TRUE. It prints out warnings and messages.

Value

A factor with same attributes (except class) as x. Does not modify x if already a factor.

Examples

# Internal function - as_factor_keep_attributes
if (FALSE) {
as_factor_keep_attributes(formatters::with_label(c(1, 1, 2, 3), "id"), verbose = FALSE)
as_factor_keep_attributes(c("a", "b", ""), "id", verbose = FALSE)
}