Skip to content

Commit

Permalink
Don't warn if fct_other() doesn't replace (#327)
Browse files Browse the repository at this point in the history
Fixes #265
  • Loading branch information
hadley authored Jan 4, 2023
1 parent 050a867 commit 0847498
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# forcats (development version)

* `fct_other()` no longer generates a warning if no levels are replaced with
other (#265).

# forcats 0.5.2

* New `fct()` which works like `factor()` but errors if values of `x`
Expand Down
4 changes: 4 additions & 0 deletions R/other.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ fct_other <- function(f, keep, drop, other_level = "Other") {
levels[levels %in% drop] <- other_level
}

if (!other_level %in% levels) {
return(f)
}

f <- lvls_revalue(f, levels)
fct_relevel(f, other_level, after = Inf)
}
6 changes: 6 additions & 0 deletions tests/testthat/test-other.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ test_that("drops levels in drop", {
expect_equal(levels(x2), c("b", "Other"))
})

test_that("works without warning if no levels replaced", {
x <- factor("a")
expect_no_warning(fct_other(x, keep = "a"))
expect_no_warning(fct_other(x, drop = "b"))
})

test_that("must supply exactly one of drop and keep", {
f <- factor(c("a", "b"))

Expand Down

0 comments on commit 0847498

Please sign in to comment.