check_class | R Documentation |
check_class
creates a specification of a recipe check that will check if
a variable is of a designated class.
check_class(
recipe,
...,
role = NA,
trained = FALSE,
class_nm = NULL,
allow_additional = FALSE,
skip = FALSE,
class_list = NULL,
id = rand_id("class")
)
recipe |
A recipe object. The check will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose variables for this check.
See |
role |
Not used by this check since no new variables are created. |
trained |
A logical for whether the selectors in |
class_nm |
A character vector that will be used in |
allow_additional |
If |
skip |
A logical. Should the check be skipped when the recipe is baked
by |
class_list |
A named list of column classes. This is |
id |
A character string that is unique to this check to identify it. |
This function can check the classes of the variables in two ways. When the
class
argument is provided it will check if all the variables specified are
of the given class. If this argument is NULL
, the check will learn the
classes of each of the specified variables in prep()
. Both ways will break
bake()
if the variables are not of the requested class. If a variable has
multiple classes in prep()
, all the classes are checked. Please note that
in prep()
the argument strings_as_factors
defaults to TRUE
. If the
train set contains character variables the check will be break bake()
when
strings_as_factors
is TRUE
.
An updated version of recipe
with the new check added to the
sequence of any existing operations.
When you tidy()
this check, a tibble with columns
terms
(the selectors or variables selected) and value
(the type)
is returned.
The underlying operation does not allow for case weights.
Other checks:
check_cols()
,
check_missing()
,
check_new_values()
,
check_range()
library(dplyr)
data(Sacramento, package = "modeldata")
# Learn the classes on the train set
train <- Sacramento[1:500, ]
test <- Sacramento[501:nrow(Sacramento), ]
recipe(train, sqft ~ .) %>%
check_class(everything()) %>%
prep(train, strings_as_factors = FALSE) %>%
bake(test)
# Manual specification
recipe(train, sqft ~ .) %>%
check_class(sqft, class_nm = "integer") %>%
check_class(city, zip, type, class_nm = "factor") %>%
check_class(latitude, longitude, class_nm = "numeric") %>%
prep(train, strings_as_factors = FALSE) %>%
bake(test)
# By default only the classes that are specified
# are allowed.
x_df <- tibble(time = c(Sys.time() - 60, Sys.time()))
x_df$time %>% class()
## Not run:
recipe(x_df) %>%
check_class(time, class_nm = "POSIXt") %>%
prep(x_df) %>%
bake_(x_df)
## End(Not run)
# Use allow_additional = TRUE if you are fine with it
recipe(x_df) %>%
check_class(time, class_nm = "POSIXt", allow_additional = TRUE) %>%
prep(x_df) %>%
bake(x_df)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.