Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fct_reordern() (Fix #16) #220

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export(fct_relabel)
export(fct_relevel)
export(fct_reorder)
export(fct_reorder2)
export(fct_reordern)
export(fct_rev)
export(fct_shift)
export(fct_shuffle)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# forcats (development version)
* `fct_reordern()` is a new function to order based on an arbitrary number of
values (@billdenney, #16)

# forcats 0.5.2

Expand Down
22 changes: 22 additions & 0 deletions R/reorder.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#' @param .desc Order in descending order? Note the default is different
#' between `fct_reorder` and `fct_reorder2`, in order to
#' match the default ordering of factors in the legend.
#' @importFrom stats median
#' @family Reordering
#' @export
#' @examples
#' df <- tibble::tribble(
Expand Down Expand Up @@ -73,6 +75,26 @@ fct_reorder2 <- function(.f, .x, .y, .fun = last2, ..., .desc = TRUE) {
lvls_reorder(.f, order(summary, decreasing = .desc))
}

#' Reorder factor levels by sorting along multiple variables
#'
#' @param .f A factor (or character vector).
#' @param ordered Passed to \code{\link{fct_inorder}()}
#' @inheritDotParams base::order
#' @family Reordering
#' @examples
#' A <- c(3, 3, 2, 1)
#' B <- c("A", "B", "C", "D")
#' fct_reordern(c("A", "B", "C", "D"), A, B)
#' fct_reordern(c("A", "B", "C", "D"), dplyr::desc(A), dplyr::desc(B))
#' fct_reordern(c("A", "B", "C", "D"), A, dplyr::desc(B))
#' @export
fct_reordern <- function(.f, ..., ordered = NA) {
f <- check_factor(.f)
new_order <- base::order(...)
idx <- as.integer(f)[new_order]
lvls_reorder(f, idx = idx[!duplicated(idx)], ordered = ordered)
}

check_single_value_per_group <- function(x, fun_arg, call = caller_env()) {
# This is a bit of a weak test, but should detect the most common case
# where `.fun` returns multiple values.
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ reference:
- fct_relevel
- fct_inorder
- fct_reorder
- fct_reordern
- fct_infreq
- fct_shuffle
- fct_rev
Expand Down
5 changes: 5 additions & 0 deletions man/fct_reorder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions man/fct_reordern.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/testthat/test-reorder.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,29 @@ test_that("fct_inseq gives error for non-numeric levels", {
f <- factor(c("c", "a", "a", "b"))
expect_error(levels(fct_inseq(f)), "level must be coercible to numeric")
})

test_that("fct_reordern works for all scenarios (fix #16)", {
A <- c(3, 3, 2, 1)
B <- c("A", "B", "C", "D")
f <- c("A", "B", "C", "D")
f_factor_ordered <- factor(f, ordered = TRUE)
expect_equal(
fct_reordern(f, A, B),
factor(f, levels = c("D", "C", "A", "B"))
)
# Ensure interaction with dplyr::desc() is accurate.
desc <- function(x) -xtfrm(x)
expect_equal(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the tests for decreasing = TRUE because you are just testing the behaviour of an existing function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed except for one to confirm that interaction between the two works as decreasing sorting is an important behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That adds a dependency on dplyr — instead you'll need to implement a local version of desc() inside the test, e.g. desc <- function(x) -xtfrm(x)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my education, I thought that since the dplyr call is in a test and dplyr is already in the DESCRIPTION Suggests field, I thought that this was OK. (Similarly, since I have it in the examples in the help, I thought that the same applied there.) I'm happy to implement that way, but my hope was that this would be simpler for the user (via help) and future maintainer (via tests) to use.

Whichever way you confirm, I'll update to that.

fct_reordern(f, A, desc(B)),
factor(f, levels = c("D", "C", "B", "A"))
)
# Checks of ordering
billdenney marked this conversation as resolved.
Show resolved Hide resolved
expect_equal(
fct_reordern(f, A, B, ordered = NA),
factor(f, levels = c("D", "C", "A", "B"))
)
expect_equal(
fct_reordern(f, A, B, ordered = TRUE),
factor(f, levels = c("D", "C", "A", "B"), ordered = TRUE)
)
})