How to Perform a SUMIF Function in R?

19 Dec, 2021
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.
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 dataframedata = 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 marksprint(aggregate(marks ~ subjects, data, sum))  # sumif operation on subjects by# performing group to get sum of idprint(aggregate(id~ subjects, data, sum))
Output:
  subjects marks
1     java   190
2      php   274

  subjects id
1     java  4
2      php 11
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 dataframedata=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 marksprint(aggregate(cbind(marks,id)~ subjects, data, sum))
Output:
  subjects marks id
1     java   190  4
2      php   274 11
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 dataframedata=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 columnsprint(aggregate(. ~ subjects, data, sum))
Output:
  subjects id marks
1     java  4   190
2      php 11   274
Picked
R-Functions
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
R - if statement
Logistic Regression in R Programming
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to import an Excel File into R ?
Joining of Dataframes in R Programming