The Wayback Machine - https://web.archive.org/web/20240828110800/https://www.geeksforgeeks.org/group-by-function-in-r-using-dplyr/
Open In App

Group by function in R using Dplyr

Last Updated : 31 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:



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.packages("dplyr")What is tibble? A tibble is a data
4 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 C/C++ Code # installing required libraries library(
2 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 the dplyr package in the working console, and then t
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 installed into the working directory using the follow
2 min read
Case when statement in R Dplyr Package using case_when() Function
This article focuses upon the case when statement in the R programming language using the case_when() function from the Dplyr package. Case when is a mechanism using which we can vectorize a bunch of if and else if statements. In simple words, using a case when statement we evaluate a condition expression, and based on that we make decisions. For e
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 to the number of inputs if n number of inputs are t
7 min read
Apply a Function (or functions) across Multiple Columns using dplyr in R
Data processing and manipulation are one of the core tasks in data science and machine learning. R Programming Language is one of the widely used programming languages for data science, and dplyr package is one of the most popular packages in R for data manipulation. In this article, we will learn how to apply a function (or functions) across multi
10 min read
R Dplyr Distinct() Function
The dplyr package in R Programming Language offers a powerful tool, the distinct() function, designed to identify and eliminate duplicate rows in a data frame. This article describes the syntax and advantages of distinct(). We'll look at some examples using an employee data frame to understand the various ways in which distinct() can be used for da
5 min read
dplyr arrange() Function in R
In data analysis and manipulation, arranging data according to specific criteria is a fundamental operation. Whether it's sorting a dataset by a certain column or multiple columns, this task is often essential for gaining insights and making informed decisions. In R Programming Language the dplyr package provides a powerful set of tools for data ma
3 min read
dplyr::count() Function in R
The count() function in the dplyr package is used to count the number of occurrences of unique combinations of variables in a data frame. It is particularly useful for generating frequency tables or summarizing categorical data. Here's a detailed explanation of how to use count() in R Programming Language. Introduction to count() function in RThe c
3 min read
Single-Table Analysis with dplyr using R Language
The dplyr package is used to perform simulations in the data by performing manipulations and transformations. It can be installed into the working space using the following command : install.packages("dplyr") Let's create the main dataframe: C/C++ Code #installing the required libraries library(dplyr) #creating a data frame data_frame = data.frame(
5 min read
Reorder the column of dataframe in R using Dplyr
In this article, we will discuss how to rearrange or reorder the column of the dataframe using dplyr package in R Programming Language. Creating Dataframe for demonstration: C/C++ Code # load the package library(dplyr) # create the dataframe with three columns # id , department and salary with 8 rows data = data.frame(id = c(7058, 7059, 7060, 7089,
4 min read
Select Random Samples in R using Dplyr
In this article, we will be looking at different methods for selecting random samples from the Dplyr package of the R programming language. To install and import the Dplyr package in the R programming language, the user needs to follow the syntax: Syntax: install.packages("dplyr") library(dplyr) Method 1: Using Sample_n() function Sample_n() functi
2 min read
Dplyr - Groupby on multiple columns using variable names in R
The group_by() method is used to group the data contained in the data frame based on the columns specified as arguments to the function call. The group_by() function takes as an argument, the across and all of the methods which has to be applied on the specified grouping over all the columns of the data frame. Syntax: group_by(col1, col2,..) This i
2 min read
Intersection of dataframes using Dplyr in R
In this article, we will discuss how to find the Intersection of two dataframes using the Dplyr package in R programming language. Dplyr provides intersect() method to get the common data in two dataframes. Syntax: intersect(dataframe1,dataframe2,dataframe3,........,dataframe n) We can perform this operation on the two dataframes. Example 1: R prog
1 min read
Get difference of dataframes using Dplyr in R
In this article, we will discuss How to find the difference between two dataframes using the Dplyr package in the R programming language. Set difference refers to getting or extracting those values from one dataset that are not present in the other. For this, dplyr supports a function called setdiff(). setdiff() is used to return the data present i
1 min read
Select variables (columns) in R using Dplyr
In this article, we are going to select variables or columns in R programming language using dplyr library. Dataset in use: Select column with column name Here we will use select() method to select column by its name Syntax: select(dataframe,column1,column2,.,column n) Here, data frame is the input dataframe and columns are the columns in the dataf
5 min read
How to Remove a Column using Dplyr package in R
In this article, we are going to remove a column(s) in the R programming language using dplyr library. Dataset in use: Remove column using column nameHere we will use select() method to select and remove column by its name. Syntax: select(dataframe,-column_name) Here, dataframe is the input dataframe and column_name is the column in the dataframe t
3 min read
How to Remove a Column by name and index using Dplyr Package in R
In this article, we are going to remove columns by name and index in the R programming language using dplyr package. Dataset in use: Remove a column by using column name We can remove a column with select() method by its column name. Syntax: select(dataframe,-column_name) Where, dataframe is the input dataframe and column_name is the name of the co
2 min read
Drop multiple columns using Dplyr package in R
In this article, we will discuss how to drop multiple columns using dplyr package in R programming language. Dataset in use: Drop multiple columns by using the column name We can remove a column with select() method by its column name Syntax: select(dataframe,-c(column_name1,column_name2,.,column_name n) Where, dataframe is the input dataframe and
4 min read
Remove Duplicate rows in R using Dplyr
In this article, we are going to remove duplicate rows in R programming language using Dplyr package. Method 1: distinct() This function is used to remove the duplicate rows in the dataframe and get the unique data Syntax: distinct(dataframe) We can also remove duplicate rows based on the multiple columns/variables in the dataframe Syntax: distinct
3 min read
Rename the column name in R using Dplyr
In this article, we are going to rename the column name using dplyr package in the R programming language. Dataset in use: Method 1: Using rename() This method is used to rename the columns in the dataframe Syntax: rename(dataframe,new_columnname=old_column,.............,name,new_columnname=old_columnname) Where dataframe is the input dataframe, ne
3 min read
Filter data by multiple conditions in R using Dplyr
In this article, we will learn how can we filter dataframe by multiple conditions in R programming language using dplyr package. The filter() function is used to produce a subset of the data frame, retaining all rows that satisfy the specified conditions. The filter() method in R programming language can be applied to both grouped and ungrouped dat
3 min read
Sorting DataFrame in R using Dplyr
In this article, we will discuss about how to sort a dataframe in R programming language using Dplyr package. The package Dplyr in R programming language provides a function called arrange() function which is useful for sorting the dataframe. Syntax : arrange(.data, ...) The methods given below show how this function can be used in various ways to
3 min read
Filter multiple values on a string column in R using Dplyr
In this article we will learn how to filter multiple values on a string column in R programming language using dplyr package. Method 1: Using filter() method filter() function is used to choose cases and filtering out the values based on the filtering conditions. Syntax: filter(df, condition) Parameters: df: Dataframe object condition: filtering ba
3 min read
Filter or subsetting rows in R using Dplyr
In this article, we are going to filter the rows from dataframe in R programming language using Dplyr package. Dataframe in use: Method 1: Subset or filter a row using filter() To filter or subset row we are going to use the filter() function. Syntax: filter(dataframe,condition) Here, dataframe is the input dataframe, and condition is used to filte
6 min read
Remove duplicate rows based on multiple columns using Dplyr in R
In this article, we will learn how to remove duplicate rows based on multiple columns using dplyr in R programming language. Dataframe in use: lang value usage 1 Java 21 21 2 C 21 21 3 Python 3 0 4 GO 5 99 5 RUST 180 44 6 Javascript 9 48 7 Cpp 12 53 8 Java 21 21 9 Julia 6 6 10 Typescript 0 8 11 Python 3 0 12 GO 6 6Removing duplicate rows based on t
4 min read
Filtering row which contains a certain string using Dplyr in R
In this article, we will learn how to filter rows that contain a certain string using dplyr package in R programming language. Functions Used Two main functions which will be used to carry out this task are: filter(): dplyr package's filter function will be used for filtering rows based on condition Syntax: filter(df , condition) Parameter : df: Th
4 min read
Sum Across Multiple Rows and Columns Using dplyr Package in R
In this article, we are going to see how to sum multiple Rows and columns using Dplyr Package in R Programming language. The dplyr package is used to perform simulations in the data by performing manipulations and transformations. It can be installed into the working space using the following command : install.packages("dplyr")Calculating row sums
3 min read
Summarise multiple columns using dplyr in R
In this article, we will discuss how to summarise multiple columns using dplyr package in R Programming Language, Method 1: Using summarise_all() method The summarise_all method in R is used to affect every column of the data frame. The output data frame returns all the columns of the data frame where the specified function is applied over every co
3 min read
Article Tags :