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
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.