Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a tibble is grouped or not

Tags:

r

dplyr

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?

like image 404
YCR Avatar asked Mar 07 '17 18:03

YCR


People also ask

How do you know if your Tibble is grouped?

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

How can you tell if an object is a Tibble?

You can use the function is_tibble() to check whether a data frame is a tibble or not. The mtcars data frame is not a tibble. But the diamonds and flights data are tibbles. More generally, you can use the class() function to find out the class of an object.

What does ungroup do in R?

Running ungroup() will drop any grouping. This can be reinstated again with regroup().


2 Answers

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")
like image 70
Konrad Avatar answered Oct 28 '22 03:10

Konrad


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)
like image 43
Steven Livingstone Avatar answered Oct 28 '22 04:10

Steven Livingstone