parse_factor
is similar to factor()
, but will generate
warnings if elements of x
are not found in levels
.
parse_factor(x, levels = NULL, ordered = FALSE, na = c("", "NA"), locale = default_locale(), include_na = TRUE, trim_ws = TRUE) col_factor(levels = NULL, ordered = FALSE, include_na = FALSE)
x | Character vector of values to parse. |
---|---|
levels | Character vector providing set of allowed levels. if |
ordered | Is it an ordered factor? |
na | Character vector of strings to interpret as missing values. Set this
option to |
locale | The locale controls defaults that vary from place to place.
The default locale is US-centric (like R), but you can use
|
include_na | If |
trim_ws | Should leading and trailing whitespace be trimmed from each field before parsing it? |
Other parsers: col_skip
,
cols_condense
, cols
,
parse_datetime
, parse_guess
,
parse_logical
, parse_number
,
parse_vector
#> [1] a b #> Levels: a b c d e f g h i j k l m n o p q r s t u v w x y zx <- 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)#> Warning: 1 parsing failure. #> row col expected actual #> 3 -- value in level set caw# Using an argument of `NULL` will generate levels based on values of `x` x2 <- parse_factor(x, levels = NULL)