VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-calculate-the-sum-by-group-in-r/

⇱ How to Calculate the Sum by Group in R? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Calculate the Sum by Group in R?

Last Updated : 23 Jul, 2025

In this article, we are going to see how to calculate the Sum by Group in R Programming Language.

Data for Demonstration

Output:

Sub Marks Add_on
Math 8 3
Math 2 1
Phy 4 9
Phy 9 4
Phy 9 7
Che 7 8
Che 1 2

Method 1: Using aggregate() method in Base R

aggregate() function is used to get the summary statistics of the data by group. The statistics include mean, min, sum. max etc.

Syntax: aggregate(dataframe$aggregate_column, list(dataframe$group_column), FUN)  

where

  • dataframe is the input dataframe.
  • aggregate_column is the column to be aggregated in the dataframe.
  • group_column is the column to be grouped with FUN.
  • FUN represents sum/mean/min/ max.

Output:

Group.1 x
Che 8
Math 10
Phy 22

Group.1 x
Che 10
Math 4
Phy 20

Method 2: Using dplyr() package

group_by() function followed by summarise() function with an appropriate action to perform.

Output:

Sub name
Che 8
Math 10
Phy 22

Method 3: Using data.table

data.table package to calculate the sum of points scored by a team.

Output:

Sub sum
Math 10
Phy 22
Che 8
Comment

Explore