Skip to content

[FEAT] Equivalent to group_by(.drop = FALSE) in group_by() and group_by_vars() #775

@phgrosjean

Description

@phgrosjean

Currently, group_by() and group_by_vars() do not allow to compute groups, keeping levels that are not present in the dataset, something like what dplyr::group_by(.drop = FALSE) does.
Yet, using groups from group_by() into fsummarise() or converting them to a GRP object shows that {collapse} honors .drop = FALSE. So, I guess it could be added as an argument to group_by() and group_by_vars() (and GRP.default()) quite easily?

Reproducible example:

library(dplyr)
library(collapse)

iris2 <- iris[iris$Species != "versicolor", ]

# dplyr, do not drop unused factor levels
iris2 |>
  group_by(Species, .drop = FALSE) |>
  summarise(mean = mean(Sepal.Length))

# collapse... how to do the same with fgroup_by() or group_by_vars()?
iris2 |>
  fgroup_by(Species) |>
  fsummarise(mean = fmean(Sepal.Length))

# Yet, collapse is handling it correctly when using group_by():
iris2 |>
  group_by(Species, .drop = FALSE) |>
  fsummarise(mean = fmean(Sepal.Length))

# Indeed, conversion from grouped_df to GRP is considering .drop = FALSE
iris2 |>
  group_by(Species, .drop = TRUE) |>
  GRP()

iris2 |>
  group_by(Species, .drop = FALSE) |>
  GRP()

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions