parse_factor is similar to factor(), but will generate warnings if elements of x are not found in levels.

parse_factor(x, levels, ordered = FALSE, na = c("", "NA"),
  locale = default_locale(), include_na = TRUE, trim_ws = TRUE)

col_factor(levels, ordered = FALSE, include_na = FALSE)

Arguments

x

Character vector of values to parse.

levels

Character vector providing set of allowed levels. if NULL, will generate levels based on the unique values of x, ordered by order of appearance in x.

ordered

Is it an ordered factor?

include_na

If NA are present, include as an explicit factor to level?

See also

Other parsers: parse_guess, parse_logical, parse_number

Examples

parse_factor(c("a", "b"), letters)
#> Error in warn_problems(parse_vector_(x, collector, na = na, locale_ = locale, trim_ws = trim_ws)): could not find function "warn_problems"
x <- c("cat", "dog", "caw") levels <- c("cat", "dog", "cow") # Base R factor() silently converts unknown levels to NA x1 <- factor(x, levels) # parse_factor generates a warning & problems x2 <- parse_factor(x, levels)
#> Error in warn_problems(parse_vector_(x, collector, na = na, locale_ = locale, trim_ws = trim_ws)): could not find function "warn_problems"
# Using an argument of `NULL` will generate levels based on values of `x` x2 <- parse_factor(x, levels = NULL)
#> Error in warn_problems(parse_vector_(x, collector, na = na, locale_ = locale, trim_ws = trim_ws)): could not find function "warn_problems"