-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Functionality to slice_* and order_by multiple columns? #6176
Comments
We get a different error with the dev version. library(dplyr, warn.conflicts = FALSE)
# Create dummy table
(df <- tibble(
col_1 = c(1, 2, 1, 2, 1, 2, 1, 2),
col_2 = c(2, 1, 0, 3, 2, 1, 0, 1)
))
#> # A tibble: 8 × 2
#> col_1 col_2
#> <dbl> <dbl>
#> 1 1 2
#> 2 2 1
#> 3 1 0
#> 4 2 3
#> 5 1 2
#> 6 2 1
#> 7 1 0
#> 8 2 1
df %>%
slice_max(order_by = c(col_1, col_2))
#> Error in `slice_max()`:
#> ! Problem while computing indices.
#> Caused by error:
#> ! `order_by` must have size 8, not size 16.
df %>%
slice_max(order_by = c(col_1, -col_2))
#> Error in `slice_max()`:
#> ! Problem while computing indices.
#> Caused by error:
#> ! `order_by` must have size 8, not size 16. This happens because |
it is supposed to be one vector, it gets expanded in: slice(.data, local({
order_by <- {{ order_by }}
n <- dplyr::n()
order_by <- fix_call(vec_assert(order_by, size = n, arg = "order_by"), NULL)
idx(order_by, n)
})) I tried if (with_ties) {
idx <- function(x, n) head(
order(x, decreasing = TRUE), smaller_ranks(desc(x), size(n))
)
} else {
idx <- function(x, n) head(order(x, decreasing = TRUE), size(n))
} which gives: library(dplyr, warn.conflicts = FALSE)
# Create dummy table
(df <- tibble(
col_1 = c(1, 2, 1, 2, 1, 2, 1, 2),
col_2 = c(2, 1, 0, 3, 2, 1, 0, 1)
))
#> # A tibble: 8 × 2
#> col_1 col_2
#> <dbl> <dbl>
#> 1 1 2
#> 2 2 1
#> 3 1 0
#> 4 2 3
#> 5 1 2
#> 6 2 1
#> 7 1 0
#> 8 2 1
df %>%
slice_max(order_by = tibble(col_1, col_2))
#> Warning in xtfrm.data.frame(x): cannot xtfrm data frames
#> Warning in xtfrm.data.frame(x): cannot xtfrm data frames
#> # A tibble: 0 × 2
#> # … with 2 variables: col_1 <dbl>, col_2 <dbl> Created on 2022-02-07 by the reprex package (v2.0.1) |
You might could swap that out |
The current constraint is the |
Yeah, I think we could use |
We'd also have to update |
I would like to be able to
order_by
multiple columns when using eitherslice_min()
orslice_max()
.This would hopefully feed into
dbplyr
and this feature request tidyverse/dbplyr#765The text was updated successfully, but these errors were encountered: