How to identify by what variable a dataset is grouped

Viewed 29

I grouped my dataset and saved it without ungrouping it. Then I loaded it and I no longer remember what variable was the grouping variable. class (mydata) give:

"grouped_df" "tbl_df"     "tbl"        "data.frame"

By what operation can I identify the grouping variable?

2 Answers

You can use group_vars or groups -

library(dplyr)

res <- mtcars %>% group_by(cyl, am)

group_vars(res)
#[1] "cyl" "am" 

groups(res)
#[[1]]
#cyl

#[[2]]
#am

We can use cur_group

library(dplyr)
mtcars %>% 
    group_by(cyl, am) %>% 
    summarise(out = names(cur_group()))
Related