How to Aggregate Multiple Columns in R?
Last Updated :
19 Dec, 2021
In this article, we will discuss how to aggregate multiple columns in R Programming Language.
Aggregation means combining two or more data. Here we are going to use the aggregate function to get the summary statistics for one or more variables in a data frame.
Syntax:
aggregate(sum_column ~ group_column, data, FUN)
where,
- data is the input dataframe
- sum_column is the column that can summarize
- group_column is the column to be grouped.
- FUN refers to functions like sum, mean, min, max, etc.
Example:
Let's create a dataframe
R
# create the dataframe with 4 columns
data = data.frame(subjects=c("java", "python", "java",
"java", "php", "php"),
id=c(1, 2, 3, 4, 5, 6),
names=c("manoj", "sai", "mounika",
"durga", "deepika", "roshan"),
marks=c(89, 89, 76, 89, 90, 67))
# display
data
Output:

Example 1: Summarize One Variable & Group by One Variable
Here, we are going to get the summary of one variable by grouping it with one variable.
Syntax:
aggregate(sum_column ~ group_column, data, FUN=sum)
In this example, We are going to use the sum function to get some of marks by grouping with subjects.
R
# create the dataframe with 4 columns
data = data.frame(subjects=c("java", "python", "java",
"java", "php", "php"),
id=c(1, 2, 3, 4, 5, 6),
names=c("manoj", "sai", "mounika",
"durga", "deepika", "roshan"),
marks=c(89, 89, 76, 89, 90, 67))
# get sum of marks by grouping with subjects
aggregate(marks~ subjects, data, FUN=sum)
Output:

Example 2: Summarize One Variable & Group by Multiple Variables
Here we are going to get the summary of one variable by grouping it with one or more variables. We have to use the + operator to group multiple columns.
Syntax:
aggregate(sum_column ~ group_column1+group_column2+...............group_columnn, data, FUN=sum)
In this example, We are going to group names and subjects to get sum of marks.
R
# create the dataframe with 4 columns
data = data.frame(subjects=c("java", "python", "java",
"java", "php", "php"),
id=c(1, 2, 3, 4, 5, 6),
names=c("manoj", "sai", "mounika",
"durga", "deepika", "roshan"),
marks=c(89, 89, 76, 89, 90, 67))
# get sum of marks by grouping with subjects and names
aggregate(marks~ subjects+names, data, FUN=sum)
Output:

Example 3: Summarize Multiple Variables & Group by One Variable
Here we are going to get the summary of one or more variables by grouping with one variable. We will use cbind() function known as column binding to get a summary of multiple variables.
Syntax:
aggregate(cbind(sum_column1,sum_column2,.,sum_column n) ~ group_column1+group_column2+...............group_columnn, data, FUN=sum)
In this example, We are going to get sum of marks and id by grouping with subjects.
R
# create the dataframe with 4 columns
data = data.frame(subjects=c("java", "python", "java",
"java", "php", "php"),
id=c(1, 2, 3, 4, 5, 6),
names=c("manoj", "sai", "mounika",
"durga", "deepika", "roshan"),
marks=c(89, 89, 76, 89, 90, 67))
# get sum of marks and id by grouping with subjects
aggregate(cbind(marks, id)~ subjects, data, FUN=sum)
Output:

Example 4: Summarize Multiple Variables & Group by Multiple Variables
Here, we are going to get the summary of one or more variables by grouping them with one or more variables. We can use cbind() for combining one or more variables and the '+' operator for grouping multiple variables.
Syntax:
aggregate(cbind(sum_column1,.,sum_column n)~ group_column1+....+group_column n, data, FUN=sum)
In this example, We are going to get sum of marks and id by grouping them with subjects and names.
R
# create the dataframe with 4 columns
data = data.frame(subjects=c("java", "python", "java",
"java", "php", "php"),
id=c(1, 2, 3, 4, 5, 6),
names=c("manoj", "sai", "mounika",
"durga", "deepika", "roshan"),
marks=c(89, 89, 76, 89, 90, 67))
# get sum of marks and id by grouping
# with subjects and names
aggregate(cbind(marks, id)~ subjects+names, data, FUN=sum)
Output:
Similar Reads
How to merge multiple DataFrames in R ?
In this article, we will discuss how to merge multiple dataframes in R Programming Language. Dataframes can be merged both row and column wise, we can merge the columns by using cbind() function and rows by using rbind() function Merging by Columns cbind() is used to combine the dataframes by column
2 min read
How to Set Column Names within the aggregate Function in R
In this article we will discuss how to set column names with the aggregate function in R programming language. The aggregate method in base R is used to divide the data frame into smaller subsets and compute a summary statistics for each of the formed groups. The function to be applied can be sum, m
3 min read
How to select multiple DataFrame columns by name in R ?
In this article, we will discuss how to select multiple columns from a DataFrame by name in R Programming Language. To get multiple columns we will use the list data structure. By using a list we can pass the dataframe columns separated with a comma. Then, we can get list by using list() function Sy
1 min read
Calculate mean of multiple columns of R DataFrame
Mean is a numerical representation of the central tendency of the sample in consideration. In this article, we are going to calculate the mean of multiple columns of a dataframe in R Programming Language. Formula: Mean= sum of observations/total number of observations. Method 1: Using colMeans() fun
2 min read
Sum of Two or Multiple DataFrame Columns in R
In this article, we will discuss how to perform some of two and multiple dataframes columns in R programming language. Database in use: Sum of two columns The columns whose sum has to be calculated can be called through the $ operator and then we can perform the sum of two dataframe columns by using
2 min read
How to Combine Lists in R
In this article, we will discuss to see how to combine the Lists in R programming language. Method 1: Using c() function We can combine lists by appending the lists using c() function. Syntax: c(list1,list2,list3,..,list n) Where lists is the multiple lists Example: R program to combine 4 listsR # c
2 min read
Insert multiple rows in R DataFrame
In this article, we are going to see how to insert multiple rows in the dataframe in R Programming Language. First, let's create a DataFrame To create a data frame we need to use vectors. We need to create vectors with some values and pass the vectors into data.frame() function as parameter. Thus, a
4 min read
How To Merge Two DataFrames in R ?
In this article, We are going to see how to merge two R dataFrames. Merging of Data frames in R can be done in two ways. Merging columnsMerging rowsMerging columns In this way, we merge the database horizontally. We use the merge function to merge two frames by one or more common key variables(i.e.,
2 min read
How to add column to dataframe in R ?
In this article, we are going to see how to add columns to dataframe in R. First, let's create a sample dataframe. Adding Column to the DataFrame We can add a column to a data frame using $ symbol. syntax: dataframe_name $ column_name = c( value 1,value 2 . . . , value n)Â Here c() function is a vec
2 min read
How to add a column based on other columns in R DataFrame ?
A data frame can be accessed and modified to store new insertions and deletions. The data frame can undergo mutations to increase its dimensions and store more data, as well as the rows and columns values, which can be modified based on other cell values. In this article, we will see how to add col
5 min read