Skip to contents

This inserts explicit missings in a factor based on a condition. Note that also additional existing NA values will be explicitly converted to given na_level.

Usage

fct_explicit_na_if(x, condition, na_level = "<Missing>")

Arguments

x

(factor)
the original factor.

condition

(logical)
where to insert missings.

na_level

(string)
which level to use for missings.

Value

The modified factor with inserted and existing NA converted to na_level.

See also

forcats::fct_explicit_na() which is used internally.

Examples

fct_explicit_na_if(factor(c("a", "b", NA)), c(TRUE, FALSE, FALSE))
#> [1] <Missing> b         <Missing>
#> Levels: a b <Missing>