From 455469193ab97115f1955f639717fb42a74f2c04 Mon Sep 17 00:00:00 2001 From: ayogasekaram Date: Wed, 5 Jun 2024 14:03:06 +0000 Subject: [PATCH] update to pass GH checks --- DESCRIPTION | 4 +++- R/standalone-forcats.R | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0f9e6f3..cf9edc4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,9 @@ Imports: tidyr Suggests: testthat (>= 3.2.0), - usethis + usethis, + forcats, + stringr Config/testthat/edition: 3 Config/testthat/parallel: true Encoding: UTF-8 diff --git a/R/standalone-forcats.R b/R/standalone-forcats.R index e705659..5c02d71 100644 --- a/R/standalone-forcats.R +++ b/R/standalone-forcats.R @@ -41,8 +41,13 @@ fct_rev <- function(f) { ) } + fct_expand <- function(f, ..., after = Inf) { - f <- levels(f) |> append(values = c(...), after = after) + old_levels <- levels(f) + new_levels <- as.character(...) + new_levels <- append(old_levels, setdiff(new_levels, old_levels), + after = after) + f <- factor(f, levels = unique(new_levels)) return(f) } @@ -54,7 +59,7 @@ fct_na_value_to_level <- function(f, level = NA) { # Add the new level to the factor's levels levels(f) <- c(levels(f), level_char) } - inds <- is.na(f) + inds <- is.na(f) if (any(inds)) { f[inds] <- level_char } @@ -62,7 +67,5 @@ fct_na_value_to_level <- function(f, level = NA) { return(factor(f, levels = levels(f))) } - - # nocov end # styler: on