Skip to content

Commit

Permalink
Respect requested variable order in distinct() (#6346)
Browse files Browse the repository at this point in the history
Fixes #6156
  • Loading branch information
hadley authored Jul 22, 2022
1 parent d7b9ae6 commit 444c139
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dplyr (development version)

* `distinct()` returns columns ordered the way you request, not the same
as the input data (#6156).

* The `.keep`, `.before`, and `.after` arguments to `mutate()`
are no longer experimental.

Expand Down
10 changes: 6 additions & 4 deletions R/distinct.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,18 @@ distinct_prepare <- function(.data,
abort(bullets, call = error_call)
}

# Always include grouping variables preserving input order
out_vars <- intersect(names(.data), c(distinct_vars, group_vars))
# Only keep unique vars
distinct_vars <- unique(distinct_vars)
# Missing grouping variables are added to the front
new_vars <- c(setdiff(group_vars, distinct_vars), distinct_vars)

if (.keep_all) {
keep <- seq_along(.data)
} else {
keep <- out_vars
keep <- new_vars
}

list(data = .data, vars = out_vars, keep = keep)
list(data = .data, vars = new_vars, keep = keep)
}

#' @export
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/distinct.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# distinct gives a warning when selecting an unknown column (#3140)
# distinct errors when selecting an unknown column (#3140)

Code
df <- tibble(g = c(1, 2), x = c(1, 2))
Expand Down
13 changes: 9 additions & 4 deletions tests/testthat/test-distinct.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ test_that("distinct handles 0 columns edge case (#2954)", {
expect_equal(nrow(distinct(tibble())), 0L)
})

test_that("distinct preserves order of the input variables (#3195)",{
test_that("distinct respects order of the specified variables (#3195, #6156)",{
d <- data.frame(x = 1:2, y = 3:4)
expect_equal(names(distinct(d, y, x)), c("x", "y"))
expect_named(distinct(d, y, x), c("y", "x"))
})

test_that("distinct adds grouping variables to front if missing",{
d <- data.frame(x = 1:2, y = 3:4)
expect_named(distinct(group_by(d, y), x), c("y", "x"))
expect_named(distinct(group_by(d, y), x, y), c("x", "y"))
})

test_that("distinct() understands both NA variants (#4516)", {
Expand Down Expand Up @@ -169,8 +175,7 @@ test_that("distinct() preserves attributes on bare data frames (#6318)", {

# Errors ------------------------------------------------------------------


test_that("distinct gives a warning when selecting an unknown column (#3140)", {
test_that("distinct errors when selecting an unknown column (#3140)", {
expect_snapshot({
df <- tibble(g = c(1, 2), x = c(1, 2))

Expand Down

0 comments on commit 444c139

Please sign in to comment.