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

Inject maskable arguments in slice_*() #6726

Merged
merged 2 commits into from
Feb 15, 2023
Merged
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# dplyr (development version)

* `slice_sample()` now works when the input has a column named `replace`.
`slice_min()` and `slice_max()` now work when the input has columns named
`na_rm` or `with_ties` (#6725).

* A major performance regression in `case_when()` has been fixed. It is still a
little slower than in dplyr 1.0.10, but we plan to improve this further in the
future (#6674).
Expand Down
10 changes: 5 additions & 5 deletions R/slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ slice_min.data.frame <- function(.data, order_by, ..., n, prop, by = NULL, with_
order_by,
size(n),
direction = "asc",
with_ties = with_ties,
na_rm = na_rm
with_ties = !!with_ties,
na_rm = !!na_rm
)
})
)
Expand Down Expand Up @@ -280,8 +280,8 @@ slice_max.data.frame <- function(.data, order_by, ..., n, prop, by = NULL, with_
order_by,
size(n),
direction = "desc",
with_ties = with_ties,
na_rm = na_rm
with_ties = !!with_ties,
na_rm = !!na_rm
)
})
)
Expand Down Expand Up @@ -321,7 +321,7 @@ slice_sample.data.frame <- function(.data, ..., n, prop, by = NULL, weight_by =
if (!is.null(weight_by)) {
weight_by <- vec_assert(weight_by, size = n, arg = "weight_by")
}
sample_int(n, size(n), replace = replace, wt = weight_by)
sample_int(n, size(n), replace = !!replace, wt = weight_by)
})
)
}
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,26 @@ test_that("slice_min/max() work with `by`", {
expect_identical(slice_max(df, x, by = g), df[c(2, 3),])
})

test_that("slice_min/max() inject `with_ties` and `na_rm` (#6725)", {
# So columns named `with_ties` and `na_rm` don't mask those arguments

df <- tibble(x = c(1, 1, 2, 2), with_ties = 1:4)

expect_identical(slice_min(df, x, n = 1), vec_slice(df, 1:2))
expect_identical(slice_min(df, x, n = 1, with_ties = FALSE), vec_slice(df, 1))

expect_identical(slice_max(df, x, n = 1), vec_slice(df, 3:4))
expect_identical(slice_max(df, x, n = 1, with_ties = FALSE), vec_slice(df, 3))

df <- tibble(x = c(1, NA), na_rm = 1:2)

expect_identical(slice_min(df, x, n = 2), df)
expect_identical(slice_min(df, x, n = 2, na_rm = TRUE), vec_slice(df, 1))

expect_identical(slice_max(df, x, n = 2), df)
expect_identical(slice_max(df, x, n = 2, na_rm = TRUE), vec_slice(df, 1))
})

test_that("slice_min/max() check size of `order_by=` (#5922)", {
expect_snapshot(error = TRUE, {
slice_min(data.frame(x = 1:10), 1:6)
Expand Down Expand Up @@ -495,6 +515,13 @@ test_that("`slice_sample()` validates `replace`", {
})
})

test_that("slice_sample() injects `replace` (#6725)", {
# So a column named `replace` doesn't mask that argument
df <- tibble(replace = 1)
expect_identical(slice_sample(df, n = 2), df)
expect_identical(slice_sample(df, n = 2, replace = TRUE), vec_slice(df, c(1, 1)))
})

test_that("slice_sample() handles positive n= and prop=", {
gf <- group_by(tibble(a = 1, b = 1), a)
expect_equal(slice_sample(gf, n = 3, replace = TRUE), gf[c(1, 1, 1), ])
Expand Down