You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
m <- mtcars %>% group_by(cyl)
# Selecting rows keeps grouping
m[1:3, ]
# Source: local data frame [3 x 11]
# Groups: cyl
#
# mpg cyl disp hp drat wt qsec vs am gear carb
# Mazda RX4 21.061601103.902.62016.460144
# Mazda RX4 Wag 21.061601103.902.87517.020144
# Datsun 71022.84108933.852.32018.611141
# Selecting columns loses grouping
m[1:3, 1:3]
# Source: local data frame [3 x 3]
# Groups:
#
# mpg cyl disp
# Mazda RX4 21.06160
# Mazda RX4 Wag 21.06160
# Datsun 71022.84108
# All attributes are lost
str(m[1:3, 1:3])
# Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 3 variables:
# $ mpg : num 212122.8
# $ cyl : num 664
# $ disp: num 160160108
This is especially problematic because R crashes if you use do() on this grouped_df which has no groups:
m[, 1:3] %>% do(mpg = mean(.$mpg))
# [segfault]
The text was updated successfully, but these errors were encountered:
wch
changed the title
Selecting columns from a grouped tbl_df with [ results in lost grouping
Selecting columns from a grouped_df with [ results in lost grouping
Apr 17, 2014
It might make sense to keep the groups of any grouping columns that are selected (cyl in this case), but drop the groups of any grouping columns that aren't selected. If no grouping columns are selected, you could drop the grouped_df class.
I realize that select() works differently -- it keeps the grouping columns, even if the user doesn't ask for them specifically.
I think we should probably protect attributes in [ methods (we wouldn't encourage users to use these methods but they are useful for developers). I'll add the code
This is especially problematic because R crashes if you use
do()
on this grouped_df which has no groups:The text was updated successfully, but these errors were encountered: