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
How to Perform a SUMIF Function in R?
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.
Contact Us