admivsn · GitHub

I would like to be able to order_by multiple columns when using either slice_min() or slice_max().

This would hopefully feed into dbplyr and this feature request tidyverse/dbplyr#765

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))
#> # A tibble: 4 × 2
#>   col_1 col_2
#>   <dbl> <dbl>
#> 1     2     1
#> 2     2     3
#> 3     2     1
#> 4     2     1
df %>%
  slice_max(order_by = c(col_1, desc(col_2)))
#> # A tibble: 4 × 2
#>   col_1 col_2
#>   <dbl> <dbl>
#> 1     2     1
#> 2     2     3
#> 3     2     1
#> 4     2     1

Read the original on github.com ↗