Skip to contents

Turn <NA> in categorical variables into "(Missing)"; numeric variables will be converted to categorical variables where numeric values as "(Observed)" and NA as "(Missing)".

Usage

missing_to_cat(data, vars, names = NULL)

Arguments

data

a dataframe with the columns to convert its missing values into categorical

vars

a character vector of the variables in data for conversion of missing values

names

a character vector of names for the new variables

Value

original dataframe containing new columns of the converted variables for the missing values with tidyverse code attached

See also

Author

Zhaoming Su

Examples

missing <- missing_to_cat(iris, vars = c("Species", "Sepal.Length"))
cat(code(missing))
#> iris |> dplyr::mutate(Species.miss = forcats::fct_na_value_to_level(Species,   "(Missing)"), .after = Species) |> dplyr::mutate(Sepal.Length.miss = factor(   dplyr::case_match(Sepal.Length, NA ~ "(Missing)", .default = "(Observed)")), .after = Sepal.Length)
head(missing)
#>   Sepal.Length Sepal.Length.miss Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1        (Observed)         3.5          1.4         0.2  setosa
#> 2          4.9        (Observed)         3.0          1.4         0.2  setosa
#> 3          4.7        (Observed)         3.2          1.3         0.2  setosa
#> 4          4.6        (Observed)         3.1          1.5         0.2  setosa
#> 5          5.0        (Observed)         3.6          1.4         0.2  setosa
#> 6          5.4        (Observed)         3.9          1.7         0.4  setosa
#>   Species.miss
#> 1       setosa
#> 2       setosa
#> 3       setosa
#> 4       setosa
#> 5       setosa
#> 6       setosa