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

use vctrs:::vec_order_locs() in group_by() and vctrs:::vec_order_radix() in arrange() #5808

Closed
wants to merge 2 commits into from
Closed
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
21 changes: 1 addition & 20 deletions R/arrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,5 @@ arrange_rows <- function(.data, dots) {

})

# we can't just use vec_compare_proxy(data) because we need to apply
# direction for each column, so we get a list of proxies instead
# and then mimic vctrs:::order_proxy
#
# should really be map2(quosures, directions, ...)
proxies <- map2(data, directions, function(column, direction) {
proxy <- vec_proxy_order(column)
desc <- identical(direction, "desc")
if (is.data.frame(proxy)) {
proxy <- order(vec_order(proxy,
direction = direction,
na_value = if(desc) "smallest" else "largest"
))
} else if(desc) {
proxy <- desc(proxy)
}
proxy
})

exec("order", !!!unname(proxies), decreasing = FALSE, na.last = TRUE)
vctrs:::vec_order_radix(data, direction = directions)
}
7 changes: 3 additions & 4 deletions R/grouped-df.r
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,9 @@ expand_groups <- function(old_groups, positions, nr) {
}

vec_split_id_order <- function(x) {
split_id <- vec_group_loc(x)
split_id$loc <- new_list_of(split_id$loc, ptype = integer())

vec_slice(split_id, vec_order(split_id$key))
split_id <- vctrs:::vec_order_locs(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DavisVaughan will this change the behaviour of group_by()? i.e. does it change the ordering of character vectors to always use the C locale?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

library(dplyr)

df <- tibble(g = c("a", "A", "B", "b"), x = 1:4)
df
#> # A tibble: 4 x 2
#>   g         x
#>   <chr> <int>
#> 1 a         1
#> 2 A         2
#> 3 B         3
#> 4 b         4

# Master
df %>%
  group_by(g) %>%
  summarise(x = x)
#> # A tibble: 4 x 2
#>   g         x
#>   <chr> <int>
#> 1 a         1
#> 2 A         2
#> 3 b         4
#> 4 B         3

# This PR
df %>%
  group_by(g) %>%
  summarise(x = x)
#> # A tibble: 4 x 2
#>   g         x
#>   <chr> <int>
#> 1 A         2
#> 2 B         3
#> 3 a         1
#> 4 b         4

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have an option to eventually sort the grouping data after the fact if really we wanted to maintain bw compat ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

group_by() could also get a .locale argument? The groups would be created and sorted in the locale specified (again, defaulting to C), which you'd see the effects of when you do a summarise()

split_id$loc <- new_list_of(split_id$loc, integer())
split_id
}

group_intersect <- function(x, new) {
Expand Down