Open In App

How to Perform a SUMIF Function in R?

Last Updated : 19 Dec, 2021
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


Similar Reads

Perform Operations over Margins of an Array or Matrix in R Programming - apply() Function
apply() function in R Language is used to perform mathematical operations across elements of an array or a matrix. Syntax: apply(x, margin, func) Parameters: x: Array or matrix margin: dimension on which operation is to be applied func: operation to be applied Example 1: # R program to illustrate # apply() function # Creating a matrix A = matrix(1:
2 min read
Perform Probability Density Analysis on t-Distribution in R Programming - dt() Function
dt() function in R Language is used to return the probability density analysis on the Student t-distribution with random variable x and degree of freedom df. Syntax: dt(x, df) Parameters: x: Random Variable df: Degree of Freedom Example 1: # R Program to perform # Probability Density Analysis # Calling dt() Function dt(0, 10) dt(1, 15) dt(3, 40) Ou
1 min read
Perform the Probability Cumulative Density Analysis on t-Distribution in R Programming - pt() Function
pt() function in R Language is used to return the probability cumulative density of the Student t-distribution. Syntax: pt(x, df) Parameters: x: Random variable df: Degree of Freedom Example 1: # R Program to perform # Cumulative Density Analysis # Calling pt() Function pt(2, 10) pt(.542, 15) Output: [1] 0.963306 [1] 0.7021105 Example 2: # R Progra
1 min read
Perform the Inverse Probability Cumulative Density Analysis on t-Distribution in R Programming - qt() Function
qt() function in R Language is used to return the inverse probability cumulative density of the Student t-distribution. Syntax: qt(x, df)Parameters: x: Random variable df: Degree of Freedom Example 1: C/C++ Code # R Program to perform Inverse # Cumulative Density Analysis # Calling qt() Function qt(.95, 10) qt(.542, 15) Output: [1] 1.812461 [1] 0.1
1 min read
Perform Linear Regression Analysis in R Programming - lm() Function
lm() function in R Language is a linear model function, used for linear regression analysis. Syntax: lm(formula) Parameters: formula: model description, such as x ~ y Example 1: # R program to illustrate # lm function # Creating two vectors x and y x <- c(rep(1:20)) y <- x * 2 # Calling lm() function to # fit a linear model f <- lm(x ~ y)
1 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_name is the column in the dataframena.rm is set to
2 min read
How to Perform Hierarchical Cluster Analysis using R Programming?
Cluster analysis or clustering is a technique to find subgroups of data points within a data set. The data points belonging to the same subgroup have similar features or properties. Clustering is an unsupervised machine learning approach and has a wide variety of applications such as market research, pattern recognition, recommendation systems, and
5 min read
How to Perform Arcsine Transformation in R?
In this article, we will discuss how to perform arcsine transformation in R programming language. Arcsine transformation is used to extend the data points in the range of 0 -1 Syntax: asin(sqrt(data)) where, data belongs to range of values from 0 to 1 Arcsine Transformation of Values in Range 0 to 1 Here we are going to create a vector with values
2 min read
How to Perform Univariate Analysis in R?
In this article, we will discuss how to perform Univariate Analysis in R Programming Language. Univariate Analysis means doing an Analysis of one variable. The analysis of univariate data is thus the simplest form of analysis since the information deals with only one quantity that changes. Example: R program to create a vector with 10 elements and
3 min read
How to Perform a VLOOKUP (Similar to Excel) in R?
VLOOKUP is a function in excel and it is an acronym for vertical lookup. The task of this function is to search for a particular value in a column to return a value from a different column but in the same row. Syntax: VLOOKUP([value], [range], [column no], [true/false]) Here, value: Specifies the value to be searchedrange: It specifies the range in
2 min read
Article Tags :