Skip to contents

Transforming Empty Strings and White Spaces to Explicit NAs while Preserving Label

Usage

h_as_factor(x, na_level = "<Missing>")

Arguments

x

(vector) input to be turned into factor with explicit missing level.

na_level

(character) the label to encode missing levels.

Value

factor with explicit NA and the same label as the input.

Details

This function preserves the label attribute.

Examples

char1 <- c(" ", "    ", "a", "b", "", "", NA)
attr(char1, "label") <- "my_label"

h_as_factor(char1)
#> [1] <Missing> <Missing> a         b         <Missing> <Missing> <Missing>
#> attr(,"label")
#> [1] my_label
#> Levels: a b <Missing>