This special class can be passed as a ptype in order to specify that the result should be a factor that contains at least the specified levels.

partial_factor(levels = character())

Arguments

levels Character vector of labels.

Examples

# Assert that x is a factor
vec_assert(factor("x"), partial_factor())

# Testing with factor() is too strict,
# because it tries to match the levels exactly
# rather than learning them from the data.
try(vec_assert(factor("x"), factor()))
#> Error : factor("x") must be a vector with type <factor<>>.
#> Instead, it has type <factor<5a425>>.
# You can also enforce a minimum set of levels
try(vec_assert(factor("x"), partial_factor("y")))
#> Error : factor("x") must be a vector with type:
#>
#>   <partial_factor<
#>     df698 {partial}
#>   >>
#>
#> Instead, it has type <factor<5a425>>.
vec_assert(factor(c("x", "y")), partial_factor("y"))

pf <- partial_factor(levels = c("x", "y"))
pf
#> partial_factor<
#>   e1304 {partial}
#> >
vec_ptype_common(factor("v"), factor("w"), .ptype = pf)
#> factor(0)
#> Levels: v w x y