Skip to content

Suggestion: Add argument keep_grouping_vars to cur_data() #5342

Description

@mayer79

The new cur_data() in dplyr is fantastic. However, it does not yet serve as a full replacement of do. Why? According to its docu, it excludes the grouping variables. I suppose this is to ensure that the same variable name does not appear twice in the resulting data.frame.

Would it be possible to add an argument keep_grouping_vars = FALSE to cur_data where this could be overwritten?

Here is an example:

library(dplyr)

iris %>% 
  group_by(Species) %>% 
  summarize(should_by_five = ncol(cur_data()))

# Output
  Species    should_by_five
  <fct>               <int>
1 setosa                  4
2 versicolor              4
3 virginica               4

This is my suggestion:

iris %>% 
  group_by(Species) %>% 
  summarize(should_by_five = ncol(cur_data(keep_grouping_vars = TRUE)))

The current workaround:

iris %>% 
  group_by(Species) %>% 
  summarize(should_by_five = ncol(bind_cols(cur_group(), cur_data())))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions