Learn R Programming

inspector (version 1.0.3)

inspect_categories: 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.

Value

inspect_categories does not return any output. There are two possible outcomes:

  • The call is silent if x is eligible to be used as the levels of a factor.

  • An informative error message is thrown otherwise.

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:

  • x is NULL or empty.

  • x is atomic.

  • x has an eligible data type (logical, integer, double, character).

  • There are NA or NaN values in x.

  • There are repeated values in x.

See Also

Examples

Run this code
# NOT RUN {
# 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]]))
# }

Run the code above in your browser using DataLab