R group_by_all() Function from dplyr

The group_by_all() function from the dplyr package in R is used to group a data frame or tibble by all its columns.

This function helps us create groups based on every column in your dataset, especially when you want to perform operations on unique combinations of all variables.

Syntax

group_by_all(.tbl, ..., .add = FALSE)

Parameters

Name Description
.tbl It is a tibble or data frame to be grouped
Additional arguments affecting the grouping.
.add It is Logical to suggest whether to add to the existing grouping or to replace it. The default is FALSE.

Return value

It returns a grouped data frame or tibble.

Example 1: Grouping by all columns

Visual Representation of R group_by_all() Function from dplyr

library(dplyr)

df <- data.frame(
  name = c("Krunal", "Ankit", "Rushabh", "Niva", "Hemang", "Vidisha"),
  score = c(85, 90, 78, 95, 80, 92),
  subject = c("Math", "Math", "History", "History", "Math", "History"),
  grade = c("10th", "11th", "11th", "10th", "11th", "10th")
)

df %>%
  group_by_all() %>%
  summarise(count = n(), .groups = "drop")

Output

Output of R group_by_all() Function

Example 2: Filtering groups after grouping by all columns

library(dplyr)

df <- data.frame(
  name = c("Krunal", "Ankit", "Rushabh", "Niva", "Hemang", "Vidisha"),
  score = c(85, 90, 78, 95, 80, 92),
  subject = c("Math", "Math", "History", "History", "Math", "History"),
  grade = c("10th", "11th", "11th", "10th", "11th", "10th")
)

# Grouping by all columns and filtering groups
df %>%
  group_by_all() %>%
  filter(score > 85)

Output

Output of Filtering groups after grouping by all columns

In this example, the data frame df is first grouped by all its columns. Then, filter() is used to retain only the groups with scores greater than 85.

Leave a Comment