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

sorting tidy selection ? #259

Closed
moodymudskipper opened this issue May 6, 2022 · 2 comments
Closed

sorting tidy selection ? #259

moodymudskipper opened this issue May 6, 2022 · 2 comments

Comments

@moodymudskipper
Copy link

There seems to be no easy way to sort tidy selections.

There are always workarounds but people tend to resort to idioms that are neither very elegant or general, some examples in this thread: https://twitter.com/antoine_fabri/status/1522328925204099072

Thanks to the flexibility of the package I could build a solution myself, would it make sense to have something like this in the package itself ?

abc <- function(..., desc = FALSE) {
  data <- tidyselect::peek_data()
  named_selection <- tidyselect::eval_select(rlang::expr(c(...)), data)
  named_selection[order(names(named_selection), named_selection, decreasing = desc)]
}

library(dplyr, w = F)
mtcars %>% 
  as_tibble() %>% 
  select(
    wt,
    abc(starts_with("c"), contains("a")),
    abc(contains("s"), desc = TRUE),
    everything()
    )
#> # A tibble: 32 × 11
#>       wt    am  carb   cyl  drat  gear    vs  qsec  disp   mpg    hp
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  2.62     1     4     6  3.9      4     0  16.5  160   21     110
#>  2  2.88     1     4     6  3.9      4     0  17.0  160   21     110
#>  3  2.32     1     1     4  3.85     4     1  18.6  108   22.8    93
#>  4  3.22     0     1     6  3.08     3     1  19.4  258   21.4   110
#>  5  3.44     0     2     8  3.15     3     0  17.0  360   18.7   175
#>  6  3.46     0     1     6  2.76     3     1  20.2  225   18.1   105
#>  7  3.57     0     4     8  3.21     3     0  15.8  360   14.3   245
#>  8  3.19     0     2     4  3.69     4     1  20    147.  24.4    62
#>  9  3.15     0     2     4  3.92     4     1  22.9  141.  22.8    95
#> 10  3.44     0     4     6  3.92     4     1  18.3  168.  19.2   123
#> # … with 22 more rows
@moodymudskipper
Copy link
Author

Another thing that would be nice if you consider this feature, is to be able to keep the original order of columns.
The | operator select the columns that match the lhs then the columns that match the rhs, this means we end up with columns that don't match the original order :

library(dplyr, warn = F)
select(starwars, starts_with("h") | starts_with("s"))
#> # A tibble: 87 × 7
#>    height hair_color    homeworld skin_color  sex    species starships
#>     <int> <chr>         <chr>     <chr>       <chr>  <chr>   <list>   
#>  1    172 blond         Tatooine  fair        male   Human   <chr [2]>
#>  2    167 <NA>          Tatooine  gold        none   Droid   <chr [0]>
#>  3     96 <NA>          Naboo     white, blue none   Droid   <chr [0]>
#>  4    202 none          Tatooine  white       male   Human   <chr [1]>
#>  5    150 brown         Alderaan  light       female Human   <chr [0]>
#>  6    178 brown, grey   Tatooine  light       male   Human   <chr [0]>
#>  7    165 brown         Tatooine  light       female Human   <chr [0]>
#>  8     97 <NA>          Tatooine  white, red  none   Droid   <chr [0]>
#>  9    183 black         Tatooine  light       male   Human   <chr [1]>
#> 10    182 auburn, white Stewjon   fair        male   Human   <chr [5]>
#> # … with 77 more rows

Maybe instead of the decreasing argument there might be an order argument that can be "ascending", "descending" or "original", defaulting to either the former or latter ?

@hadley
Copy link
Member

hadley commented Aug 10, 2022

I think this is out of scope for tidyselect because we think now of the selections as logical combinations, so there's no sense of order. We might revisit this in the future, but for now we want stay focussed purely on selection, not ordering.

@hadley hadley closed this as completed Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants