parse_factor {minty} | R Documentation |
Parse factors
Description
parse_factor()
is similar to factor()
.
Usage
parse_factor(
x,
levels = NULL,
ordered = FALSE,
na = c("", "NA"),
locale = default_locale(),
include_na = TRUE,
trim_ws = TRUE,
.return_problems = FALSE
)
col_factor(levels = NULL, ordered = FALSE, include_na = FALSE)
Arguments
x |
Character vector of values to parse. |
levels |
Character vector of the allowed levels. When |
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 (ASCII spaces and tabs) be trimmed from each field before parsing it? |
.return_problems |
Whether to hide the |
Value
a parsed vector
See Also
Other parsers:
col_skip()
,
parse_datetime()
,
parse_guess()
,
parse_logical()
,
parse_number()
,
parse_vector()
Examples
# discover the levels from the data
parse_factor(c("a", "b"))
parse_factor(c("a", "b", "-99"))
parse_factor(c("a", "b", "-99"), na = c("", "NA", "-99"))
parse_factor(c("a", "b", "-99"), na = c("", "NA", "-99"), include_na = FALSE)
# provide the levels explicitly
parse_factor(c("a", "b"), levels = letters[1:5])
x <- c("cat", "dog", "caw")
animals <- c("cat", "dog", "cow")
# base::factor() silently converts elements that do not match any levels to
# NA
factor(x, levels = animals)
# parse_factor() generates same factor as base::factor() but throws a warning
# and reports problems
parse_factor(x, levels = animals)