From 18283e43837122f6f3d6d48346704b0d1ef9c833 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Wed, 5 Jun 2024 14:50:50 -0700 Subject: [PATCH] updated fn --- R/standalone-forcats.R | 17 +++++++++-------- man/lst_scripts.Rd | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/R/standalone-forcats.R b/R/standalone-forcats.R index b34032c..c35837f 100644 --- a/R/standalone-forcats.R +++ b/R/standalone-forcats.R @@ -52,14 +52,15 @@ fct_expand <- function(f, ..., after = Inf) { } fct_na_value_to_level <- function(f, level = NA) { - if (is.na(level)) { - # Use addNA to add NA as a level - f <- addNA(f) - } else { - # Convert NA to the specified level - f[is.na(f)] <- level - } - return(f) + if (!inherits(f, "factor")) f <- factor(f) + + # make NA an explicit level + f <- addNA(f, ifany = FALSE) + + # replace NA level with the string passed in `level` argument + if (!is.na(level)) levels(f)[is.na(levels(f))] <- level + + f } diff --git a/man/lst_scripts.Rd b/man/lst_scripts.Rd index e7e6099..9f72a8a 100644 --- a/man/lst_scripts.Rd +++ b/man/lst_scripts.Rd @@ -5,7 +5,7 @@ \alias{lst_scripts} \title{Standalone Script} \format{ -An object of class \code{list} of length 15. +An object of class \code{list} of length 18. } \usage{ lst_scripts