View source: R/fct_explicit_na.R
fct_explicit_na | R Documentation |
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
).
fct_explicit_na(f, na_level = "(Missing)")
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 |
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.
A factor with missing values explicitly assigned to the specified na_level
.
fct_expand
, lvls_revalue
# 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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.