YCR
YCR

Reputation: 4002

Determine if a tibble is grouped or not

Is there a function to determine if a tibble is a grouped one or not.

I use the following code to create an aggregated variable without shrinking the dataset:

mydataset %>% select(count, group) %>%
  group_by(group) %>%
  mutate(count_group = sum(count))

If I use mutate, I have a grouped tibble. If I use summarise, I have a simple tibble.

Is there a function, like as.grouped() which allows to determine the character grouped of a tibble?

Upvotes: 18

Views: 7075

Answers (3)

Konrad
Konrad

Reputation: 18585

Information on grouping is saved as an attribute. Example:

library("tidyverse")
a <- mtcars %>% group_by(cyl)
attributes(a)
$names
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"

$row.names
 [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"
 [4] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"
 [7] "Duster 360"          "Merc 240D"           "Merc 230"
[10] "Merc 280"            "Merc 280C"           "Merc 450SE"
[13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood"
[16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"
[19] "Honda Civic"         "Toyota Corolla"      "Toyota Corona"
[22] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"
[25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-2"
[28] "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"
[31] "Maserati Bora"       "Volvo 142E"

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

$groups
# A tibble: 3 x 2
    cyl .rows
  <dbl> <list>
1     4 <int [11]>
2     6 <int [7]>
3     8 <int [14]>

attributes function can be used to check for the presence of grouping attribute:

any(names(attributes(a)) == "groups")

Upvotes: 3

Steven Livingstone
Steven Livingstone

Reputation: 176

The functions is.grouped_df() and is_grouped_df() will both return a TRUE logical if it's a grouped tibble, and FALSE if it is not.

# Create a tibble    
df <- tibble(x = c(5, 2, NA))
# Group by column 'x'
gdf <- group_by(df, x)
# Returns FALSE
is.grouped_df(df)
# Returns TRUE
is.grouped_df(gdf)

Upvotes: 16

ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19375

of course you are aware that you can use ungroup() to remove any grouping. Anyway, there is pretty way to figure out if the dataframe is grouped, and this is simply by printing the dataframe. The grouping variables will appear on top.

dataframe <- data_frame('group' = c('a','a','b'),
                        'value' = c(1,2,3))

dataframe %>% group_by(group)

Source: local data frame [3 x 2]
**Groups: group [2]**

  group value
  <chr> <dbl>
1     a     1
2     a     2
3     b     3

Upvotes: 4

Related Questions