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?
grouped_df() and is_grouped_df() will both return a TRUE logical if it's a grouped tibble, and FALSE if it is not.
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.
Running ungroup() will drop any grouping. This can be reinstated again with regroup().
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")
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With