| inspect_categories {inspector} | R Documentation |
Validate factor levels
Description
inspect_categories checks if an object is eligible to be used
as the levels of a factor. This can be useful to validate inputs in
user-defined functions.
Usage
inspect_categories(x)
Arguments
x |
An arbitrary object. |
Details
inspect_categories conducts a series of tests to check if x
is eligible to be used as the levels of a factor. Namely,
inspect_categories checks if:
-
xisNULLor empty. -
xis atomic. -
xhas an eligible data type (logical, integer, double, character). There are
NAorNaNvalues inx.There are repeated values in
x.
Value
inspect_categories does not return any output. There are two
possible outcomes:
The call is silent if
xis eligible to be used as the levels of a factor.An informative error message is thrown otherwise.
See Also
-
inspect_data_dichotomousto validate dichotomous data. -
inspect_data_categoricalandinspect_data_cat_as_dichotomto validate categorical data. -
inspect_par_bernoullito validate Bernoulli/Binomial proportions. -
inspect_par_multinomialto validate vectors of Multinomial proportions. -
inspect_characterto validate character vectors. -
inspect_character_matchto validate character vectors with predefined allowed values.
Examples
# Calls that pass silently:
x1 <- 1:5
x2 <- c("yes", "no")
x3 <- c(TRUE, FALSE)
x4 <- factor(c("smoker", "non-smoker"))
x5 <- factor(c("yes", "no", "yes"))
inspect_categories(x1)
inspect_categories(x2)
inspect_categories(x3)
inspect_categories(x4)
inspect_categories(levels(x5))
# Calls that throw informative error messages:
y1 <- c(1, 1:5)
y2 <- c("yes", "no", "yes")
y3 <- factor(c("yes", "no", "yes"))
try(inspect_categories(y1))
try(inspect_categories(y2))
try(inspect_categories(y3))
try(mylist <- list(
NULL, numeric(0),
complex(1), list(10), NaN, NA
))
try(inspect_categories(mylist[[1]]))
try(inspect_categories(mylist[[2]]))
try(inspect_categories(mylist[[3]]))
try(inspect_categories(mylist[[4]]))
try(inspect_categories(mylist[[5]]))
try(inspect_categories(mylist[[6]]))