Group_by() on a single column

This is the simplest way by which a column can be grouped, just pass the name of the column to be grouped in the group_by() function and the action to be performed on this grouped column in summarise() function.

Example: Grouping single column by group_by()

R




library(dplyr)
 
df = read.csv("Sample_Superstore.csv")
 
df_grp_region = df %>% group_by(Region)  %>%
                    summarise(total_sales = sum(Sales),
                              total_profits = sum(Profit),
                              .groups = 'drop')
 
View(df_grp_region)


Output:

 

Group by function in R using Dplyr

Group_by() function belongs to the dplyr package in the R programming language, which groups the data frames. Group_by() function alone will not give any output. It should be followed by summarise() function with an appropriate action to perform. It works similar to GROUP BY in SQL and pivot table in excel.

Syntax:

group_by(col,…)

Syntax:

group_by(col,..) %>% summarise(action)

The dataset in use:

Sample_Superstore

Similar Reads

Group_by() on a single column

This is the simplest way by which a column can be grouped, just pass the name of the column to be grouped in the group_by() function and the action to be performed on this grouped column in summarise() function....

Group_by() on multiple columns

Group_by() on multiple columns...