tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.78k stars 2.12k forks source link

Using tbl_dt on grouped tbl_dt silently ungroups (and vice versa). #1620

Closed koseri closed 8 years ago

koseri commented 8 years ago

Creating a tbl_dt from a grouped tbl_df silently ungroups the result. Similarly, creating a tbl_df from a grouped tbl_dt silently ungroups the result. This behavior seems odd given the general tendency not to ungroup; I would have expected the group information to stay.

x_df <- data_frame ( g = rep(c("a", "b", "c" ), each = 5), v = 1:15)
x_dt <- x_df %>% tbl_dt()

x_df %>% group_by(g) %>%              groups()
x_df %>% group_by(g) %>% tbl_dt() %>% groups()

x_dt %>% group_by(g) %>%              groups()
x_dt %>% group_by(g) %>% tbl_df() %>% groups()
hadley commented 8 years ago

The whole point of tbl_dt() is to make a tbl_df as opposed to a grouped_df. Otherwise how would you coerce when needed?

koseri commented 8 years ago

Confused. Shouldn't that be "...whole point of tbl_df..."?

Now I see that tbl_df ungroups tbl_dfs, too: identical ( (foo %>% tbl_df() %>% group_by(code) %>% ungroup() ), (foo %>% tbl_df() %>% group_by(code) %>% tbl_df() ) ) returns TRUE

My goal is to be able to convert back and forth between a tbl_df and a tbl_dt while preserving grouping information. Is there a different idiom for preserving groups than this:

 preserved_groups <- groups(xdt)
    xdf <- xdt %>% tbl_df() %>% group_by_ ( .dots = preserved_groups)