Open In App

How to Perform a SUMIF Function in R?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the sumif function in R Programming Language.

This function is used to group the data and get the sum of the values by a group of values in the dataframe, so we are going to perform this operation on the dataframe.

Method 1: Perform a SUMIF Function On One Column:

In this method to perform a SUMIF() function on one column, the user needs to call the aggregate function with the required parameters as mentioned below to get the results accordingly in the R language.

Syntax:

aggregate(column_sum ~ group_column, dataframe, sum)

Example:

In this example, we are going to create a dataframe with 4 columns, In the first operation, we are performing the sumif operation on subjects by performing group to get sum of marks and in the second operation, we are performing the sumif operation on subjects by performing group to get the sum of id.

R




# create a dataframe
data = data.frame(id=c(1, 2, 3, 4, 5),
                  name=c('rupa', 'rani', 'radha', 'ramu', 'roja'),
                  subjects=c('java', 'php', 'java', 'php', 'php'),
                  marks=c(100, 98, 90, 87, 89))
  
# sumif operation on subjects by
# performing group to get sum of marks
print(aggregate(marks ~ subjects, data, sum))
  
# sumif operation on subjects by
# performing group to get sum of id
print(aggregate(id~ subjects, data, sum))


Output:

  subjects marks
1     java   190
2      php   274

  subjects id
1     java  4
2      php 11

Method 2: Perform a SUMIF Function on Multiple Columns

In this approach to perform the SUMIF function on multiple columns of the given data frame the user needs to call the aggregate() function with the cbind() function as the parameters as shown in the syntax below to get the sumif function on multiple columns of the given data frame in the R programming language.

Syntax:

aggregate(cbind(column_sum1,column_sum2,..,) ~ group_column, dataframe, sum)

Example:

In this example, we will be performing the sumif operation on subjects by performing a group to get the sum of id and sum of marks in the R programming language.

R




# create a dataframe
data=data.frame(id=c(1,2,3,4,5),
                name=c('rupa','rani','radha','ramu','roja'),
                subjects=c('java','php','java','php','php'),
                marks=c(100,98,90,87,89))
  
# sumif operation on subjects by performing
# group to get sum of id and sum of marks
print(aggregate(cbind(marks,id)~ subjects, data, sum))


Output:

  subjects marks id
1     java   190  4
2      php   274 11

Method 3: Perform a SUMIF Function on All Columns

In this method to perform a sumif function on all the columns of the given dataframe, the user needs to simply call the aggregate() function of base R and pass the name of the data frame as the parameter into it as shown in the syntax below to get the result to the performing the sumif function on the entire data frame in the R language.

Syntax:

aggregate(. ~ group_column, dataframe, sum)

Example:

In this example, we are performing the sumif operation on subjects by performing a group to get the sum of all columns using the aggregate() function in the R language.

R




# create a dataframe
data=data.frame(id=c(1,2,3,4,5),
                subjects=c('java','php','java','php','php'),
                marks=c(100,98,90,87,89))
  
# sumif operation on subjects by 
# performing group to get sum of all columns
print(aggregate(. ~ subjects, data, sum))


Output:

  subjects id marks
1     java  4   190
2      php 11   274


Last Updated : 19 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads