2
votes

I intuitively expected using the ungroup() function would drop the 'vars' attribute. Sometimes it does and sometimes it doesn't. What is the rule? I'm trying to understand the internals of grouping/ungrouping.

Consider the two examples below (using dplyr 0.7.4 on R 3.4.4). In the first one the 'vars' attribute is retained. In the second it is dropped.

#Example 1
models <- mtcars %>% group_by(cyl) %>% do(mod = lm(mpg ~ disp, data = .)) 
   %>% ungroup() 
attributes(models)

$row.names [1] 1 2 3

$vars [1] "cyl"

$drop [1] TRUE

$names [1] "cyl" "mod"

$class [1] "tbl_df" "tbl" "data.frame"

#Example 2
attributes(mtcars %>% group_by(cyl) %>% count() %>% ungroup())

$class [1] "tbl_df" "tbl" "data.frame"

$names [1] "cyl" "n"

$row.names [1] 1 2 3

1

1 Answers

2
votes

The difference comes from the fact that group_by %>% do returns a rowwise_df while group_by %>% count returns a grouped_df, and for rowwise_df, ungroup only removes the rowwise_df class attribute and leaves other rowwise related attributes untouched, also see this How does one stop using rowwise in dplyr:

mtcars %>% group_by(cyl) %>% do(mod = lm(mpg ~ disp, data = .)) %>% class()
# [1] "rowwise_df" "tbl_df"     "tbl"        "data.frame"

mtcars %>% group_by(cyl) %>% count() %>% class()
# [1] "grouped_df" "tbl_df"     "tbl"        "data.frame"