How to Perform a SUMIF Function in R?
Last Updated :
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.
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
Similar Reads
How to Perform a SUMIF Function in Pandas?
sumif() function is used to perform sum operation by a group of items in the dataframe, It can be applied on single and multiple columns and we can also use this function with groupby function. Method 1: SUMIF on all columns with groupby() This function is used to display sum of all columns with res
4 min read
How to Perform a COUNTIF Function in R?
In this article, we will discuss how to perform COUNTIF function in R programming language. This is used to count the value present in the dataframe. We have to use sum() function to get the count. Syntax: sum(dataframe$column_name == value, na.rm=TRUE) where, dataframe is the input dataframecolumn_
2 min read
How to Use sum Function in R?
In this article, we will discuss how to use the sum() function in the R Programming Language. sum() function: This is used to return the total/sum of the given data Syntax: sum(data) Arguments: data can be a vector or a dataframeExample 1: Using sum() function to calculate the sum of vector elements
5 min read
How to Perform a Bonferroni Correction in R?
The Bonferroni correction is a method to adjust p-values when performing multiple comparisons to control the family-wise error rate. It is particularly useful in hypothesis testing where multiple tests are conducted simultaneously. Here's how you can perform a Bonferroni correction in R Programming
3 min read
How to use Summary Function in R?
The summary() function provides a quick statistical overview of a given dataset or vector. When applied to numeric data, it returns the following key summary statistics:Min: The minimum value in the data1st Qu: The first quartile (25th percentile)Median: The middle value (50th percentile)3rd Qu: The
2 min read
How to Use Nrow Function in R?
In this article, we will discuss how to use Nrow function in R Programming Language. This function is used in the dataframe or the matrix to get the number of rows. Syntax: nrow(data) where, data can be a dataframe or a matrix. Example 1: Count Rows in Data Frame In this example, we are going to cou
2 min read
How to Insert a Function in Excel?
In MS Excel formula is an expression that help to calculate the value of a cell and in Microsoft Excel has many inbuilt in functions that we can use in our formula. Now if you want to insert a function in Excel or want to see all the functions by category, then in this series of MS Excel tutorial gu
4 min read
Functions in R Programming
A function accepts input arguments and produces the output by executing valid R commands that are inside the function. Functions are useful when you want to perform a certain task multiple times. In R Programming Language when you are creating a function the function name and the file in which you a
8 min read
How to Perform a Chi-Square Goodness of Fit Test in R
The Chi-Square Goodness of Fit Test is a statistical test used to analyze the difference between the observed and expected frequency distribution values in categorical data. This test is popularly used in various domains such as science, biology, business, etc. In this article, we will understand ho
6 min read
sum() function in R
sum() function in R Programming Language returns the addition of the values passed as arguments to the function. Syntax: sum(...) Parameters: ...: numeric or complex or logical vectorssum() Function in R ExampleR program to add two numbersHere we will use sum() functions to add two numbers. R a1=c(1
2 min read