Group by function in R using Dplyr Last Updated : 31 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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 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() on multiple columns Group_by() function can also be performed on two or more columns, the column names need to be in the correct order. The grouping will occur according to the first column name in the group_by function and then the grouping will be done according to the second column. Example: Grouping multiple columns R library(dplyr) df = read.csv("Sample_Superstore.csv") df_grp_reg_cat = df %>% group_by(Region, Category) %>% summarise(total_Sales = sum(Sales), total_Profit = sum(Profit), .groups = 'drop') View(df_grp_reg_cat) Output: We can also calculate mean, count, minimum or maximum by replacing the sum in the summarise or aggregate function. For example, we will find mean sales and profits for the same group_by example above. Example: R library(dplyr) df = read.csv("Sample_Superstore.csv") df_grp_reg_cat = df %>% group_by(Region, Category) %>% summarise(mean_Sales = mean(Sales), mean_Profit = mean(Profit), .groups = 'drop') View(df_grp_reg_cat) Output: Comment More infoAdvertise with us Next Article Group by function in R using Dplyr P pasulakiransai Follow Improve Article Tags : R Language R Dplyr Similar Reads Apply a function to each group using Dplyr in R In this article, we are going to learn how to apply a function to each group using dplyr in the R programming language. The dplyr package in R is used for data manipulations and modifications. The package can be downloaded and installed into the working space using the following command : install.p 4 min read Windows Function in R using Dplyr Aggregation functions in R are used to take a bunch of values and give us output as a single value. Some of the examples of aggregation methods are the sum and mean. Windows functions in R provide a variation to the aggregation methods in the sense that they return the number of outputs equivalent t 7 min read How to Create Frequency Table by Group using Dplyr in R In this article, we will be looking at the approach to creating a frequency table group with its working examples in the R programming language. Create Frequency Table by Group using dplyr package: In this approach to create the frequency table by group, the user first needs to import and install th 2 min read Group by one or more variables using Dplyr in R The group_by() method is used to divide and segregate date based on groups contained within the specific columns. The required column to group by is specified as an argument of this function. It may contain multiple column names. Syntax: group_by(col1, col2, ...) Example 1: Group by one variable R # 2 min read Rank variable by group using Dplyr package in R In this article, we are going to see how to rank the variable by group using dplyr in R Programming Language. The dplyr package in R is used to perform mutations and data manipulations in R. It is particularly useful for working with data frames and data tables. The package can be downloaded and in 2 min read Like