R group_by_if() Function from dplyr

R group_by_if() function from the dplyr package allows you to group your data by columns that satisfy a given condition. This condition is defined by a predicate function that returns TRUE or FALSE for each column.

This function is helpful for scenarios where the columns for grouping depend on specific properties like data type, name, etc.

Syntax

group_by_if(.tbl, .predicate, ..., .add = FALSE)

Parameters

Name Description
.tbl It is a data frame or tibble to be grouped.
.predicate It is a function that returns TRUE or FALSE for each column. Columns for which .predicate returns TRUE are used for grouping.
Additional arguments to pass to .predicate.
.add It is a Logical argument suggesting whether to add to the existing grouping (if any). The default is FALSE.

Return value

It is a grouped data frame or tibble.

Example: Grouping by numeric columns

Visual Representation of R group_by_if() 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_if(is.numeric)

Output

Output of R group_by_if() Function

That’s all!

Leave a Comment