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

[R] count should return an ungrouped dataframe #31133

Closed
asfimport opened this issue Feb 14, 2022 · 2 comments
Closed

[R] count should return an ungrouped dataframe #31133

asfimport opened this issue Feb 14, 2022 · 2 comments

Comments

@asfimport
Copy link

Unless grouped before dplyr::count returns a ungrouped data.frame. The arrow implement preserves the grouping variables:

 

library(arrow, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
tf1 <- tempfile()
dir.create(tf1)
starwars |>
  write_dataset(tf1)

# no group ----------------------------------------------------------------
## dplyr behaviour
count_dplyr_no_group <- starwars %>%
  count(gender, homeworld, species)
group_vars(count_dplyr_no_group)
#> character(0)
## arrow behaviour
count_arrow_no_group <- open_dataset(tf1) %>%
  count(gender, homeworld, species) %>%
  collect()
group_vars(count_arrow_no_group)
#> [1] "gender"    "homeworld"

If I am correct that this is a undesired behaviour I think it can be fixed here using this patch:

 

count.arrow_dplyr_query <- function(x, ..., wt = NULL, sort = FALSE, name = NULL) {
  if (!missing(...)) {
    out <- dplyr::group_by(x, ..., .add = TRUE)
  } else {
    out <- x
  }
  out <- dplyr::tally(out, wt = {{ wt }}, sort = sort, name = name)

  gv <- dplyr::group_vars(x)
  if (rlang::is_empty(gv)) {
    out <- dplyr::ungroup(out)
  } else {
    # Restore original group vars
    out$group_by_vars <- gv
  }
  out
}

 

I can submit a PR with some tests if that would be helpful.

Reporter: Sam Albers / @boshek
Assignee: Sam Albers / @boshek

PRs and other links:

Note: This issue was originally created as ARROW-15679. Please see the migration documentation for further details.

@asfimport
Copy link
Author

Nicola Crane / @thisisnic:
Hi @boshek - thanks for reporting this!  A PR with some tests sounds great - please do, and let me know if there's anything you're not sure of, or that I can help with.

@asfimport
Copy link
Author

Ian Cook / @ianmcook:
Issue resolved by pull request 12435
#12435

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant