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