fct_explicit_na: Explicitly Assign a Level for Missing Values in a Factor

View source: R/fct_explicit_na.R

fct_explicit_naR Documentation

Explicitly Assign a Level for Missing Values in a Factor

Description

A function to handle missing values in a factor by explicitly assigning them a specified level. This is based on a deprecated function in the forcats package (forcats::fct_explicit_na).

Usage

fct_explicit_na(f, na_level = "(Missing)")

Arguments

f

A factor (or character vector). If a character vector is provided, it will be automatically converted to a factor.

na_level

A character string specifying the level to assign to missing values (NAs). Defaults to "(Missing)".

Details

  • If the factor contains missing values (NAs), they are replaced with the value specified by na_level.

  • If the factor already has an NA level (i.e., a level with NA as its name), it will be replaced by na_level.

  • If there are no missing values, the factor is returned unchanged.

Value

A factor with missing values explicitly assigned to the specified na_level.

See Also

fct_expand, lvls_revalue

Examples

# Example 1: Factor with missing values
f <- factor(c("A", "B", NA, "C"))
fct_explicit_na(f)

# Example 2: Specifying a custom level for missing values
fct_explicit_na(f, na_level = "Unknown")


emilelatour/lamisc documentation built on March 29, 2025, 1:23 p.m.