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

Is there a function to return the number of groups? #477

Closed
Mullefa opened this issue Jul 1, 2014 · 1 comment
Closed

Is there a function to return the number of groups? #477

Mullefa opened this issue Jul 1, 2014 · 1 comment
Assignees
Labels
feature a feature request or enhancement
Milestone

Comments

@Mullefa
Copy link

Mullefa commented Jul 1, 2014

Wasn't sure if there was. Currently I'm using this function:

n_groups <- function(tbl) {
  g <- groups(tbl)

  if (is.null(g)) {
    tbl_nm <- deparse(substitute(tbl))
    warning(tbl_nm, " is not grouped", immediate. = TRUE)
    0
  } else {
    g <- unlist(lapply(g, as.character))
    nrow(unique(tbl[, g, drop = FALSE]))
  }
}
@hadley
Copy link
Member

hadley commented Jul 28, 2014

Could you please give me an example of where you need this?

@hadley hadley added this to the 0.3 milestone Aug 1, 2014
@hadley hadley self-assigned this Aug 1, 2014
@hadley hadley closed this as completed in cc0026a Aug 1, 2014
krlmlr pushed a commit to krlmlr/dplyr that referenced this issue Mar 2, 2016
Also make sure group_size works for ungrouped data, and add tests
@lock lock bot locked as resolved and limited conversation to collaborators Jun 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants