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

str_rank()? #353

Closed
courtiol opened this issue Nov 18, 2020 · 3 comments
Closed

str_rank()? #353

courtiol opened this issue Nov 18, 2020 · 3 comments

Comments

@courtiol
Copy link

R base as sorting, ordering and ranking functions. Each fits a different purpose.
As far as I can see, stringr (and stringi) don't have ranking versions of their sorting and ordering functions.
One motivation could be obtaining natural sorting in dplyr:

library(dplyr, warn.conflicts = FALSE)
foo <- tibble(x = c("A100", "B3", "A1", "A2", "A10", "A11", "A100C", "AA9"))

foo %>%
  arrange(x)
#> # A tibble: 8 x 1
#>   x    
#>   <chr>
#> 1 A1   
#> 2 A10  
#> 3 A100 
#> 4 A100C
#> 5 A11  
#> 6 A2   
#> 7 AA9  
#> 8 B3

str_rank <- function(x, ...) match(x, stringr::str_sort(x, ...))

foo %>%
  arrange(str_rank(x, numeric = TRUE))
#> # A tibble: 8 x 1
#>   x    
#>   <chr>
#> 1 A1   
#> 2 A2   
#> 3 A10  
#> 4 A11  
#> 5 A100 
#> 6 A100C
#> 7 AA9  
#> 8 B3

Created on 2020-11-18 by the reprex package (v0.3.0)

Not sure this is needed in stringi since people using stringi should be capable to define, with no difficulty, a little function like the one above on the fly, but with stringr aiming at being more accessible it seems to make sense to me...

@courtiol
Copy link
Author

Note: @hadley suggested to me that stringi::stri_sort_key() does that already, but I cannot make it work, so it does not seem very user friendly.

@hadley
Copy link
Member

hadley commented Apr 18, 2021

Thanks for filing this issue! Unfortunately, since there doesn't seem to be much interest in it, I don't think it's a good fit for this package: developing good software requires relentless focus, which means that we have to say no to many good ideas. Even though I'm closing this issue, I really appreciate the feedback, and hope you'll continue to contribute in the future 😄

@hadley hadley closed this as completed Apr 18, 2021
@hadley
Copy link
Member

hadley commented Jan 23, 2022

Ok, I just needed this so I do now think it's worthwhile.

@hadley hadley reopened this Jan 23, 2022
@hadley hadley closed this as completed in b60c5ec Jan 23, 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