[Experimental]

This helper function converts all character and logical variables to factor variables in a data.frame. It also sets an explicit missing data level for all factor variables that have at least one NA.

h_df_factors_with_explicit_na(data, na_level = "<Missing>")

Arguments

data

(data.frame)
input data with at least one column.

na_level

(string)
explicit missing level to be used.

Value

The modified data.

Examples

dat <- data.frame(
  a = c(NA, 2),
  b = c("A", NA),
  c = c("C", "D"),
  d = factor(c(NA, "X")),
  e = factor(c("Y", "Z"))
)
h_df_factors_with_explicit_na(dat)
#>    a         b c         d e
#> 1 NA         A C <Missing> Y
#> 2  2 <Missing> D         X Z