Sum of column in R based on condition
Last Updated :
21 Jul, 2025
Sum of Column in R based on condition refers to calculating the total of values in a column after applying specific conditions. This helps in filtering the data before performing the summation. Logical conditions are used with indexing or the subset() function along with sum(). It supports both single and multiple conditions.
Syntax:
sum(dataframe[which(condition), column_number])
Parameters:
- dataframe: used to store tabular data in rows and columns
- condition: used to filter the rows that match a specific rule
- column_number: used to specify which column to sum (by index)
Sum of Columns Based on a Single Condition
We can calculate the sum of specific columns by applying a condition to filter the data first.
Example 1: We create a dataframe and calculate the sum of the Score
column where the Name
is equal to 'U'.
- Name, Score, Wickets: vectors used to define data for each column
- data.frame(): used to create a dataframe from vectors
- df$Name == 'U': condition to filter rows where Name is 'U'
- which(): gives the row indices where the condition is TRUE
- sum(): calculates the total of values in the selected column
R
Name = c("U", "U", "U", "V", "V", "W")
Score = c(10, 20, 30, 40, 50, 60)
Wickets = c(2, 3, 4, 5, 6, 7)
df = data.frame(Name, Score, Wickets)
sum(df[which(df$Name == 'U'), 2])
Output:
[1] 60
Example 2: We calculate the sum of the Wickets column where the Name is equal to 'V'.
- df$Name == 'V': filters rows where Name is 'V'
- 3 (column index): refers to the Wickets column
R
Name = c("U", "U", "U", "V", "V", "W")
Score = c(10, 20, 30, 40, 50, 60)
Wickets = c(2, 3, 4, 5, 6, 7)
df = data.frame(Name, Score, Wickets)
sum(df[which(df$Name == 'V'), 3])
Output:
[1] 11
Sum of Column Based on Multiple Conditions
We can calculate the sum of a column by applying multiple conditions to filter the data before performing the calculation.
Example 1: We calculate the sum of the score column based on multiple conditions: name is 'U' and place is 'uk'.
- name, place, score, wickets: vectors for dataframe
- data.frame(): used to combine vectors into tabular form
- df$name == 'U' & df$place == 'uk': filters rows where both conditions are TRUE
- 3 (column index): refers to the score column
- & operator: used to combine multiple conditions
R
name = c("U", "U", "W", "V", "V", "U")
place = c("uk", "uk", "rk", "rk", "rk", "nk")
score = c(10, 20, 30, 40, 50, 60)
wickets = c(2, 3, 4, 5, 6, 7)
df = data.frame(name, place, score, wickets)
sum(df[which(df$name == 'U' & df$place == 'uk'), 3])
Output:
[1] 30
Example 2: We now sum the score values where name is 'V' and place is 'rk'.
- df$name == 'V' & df$place == 'rk': applies multiple conditions
- sum(): adds up matching values in the score column
R
name = c("U", "U", "W", "V", "V", "U")
place = c("uk", "uk", "rk", "rk", "rk", "nk")
score = c(10, 20, 30, 40, 50, 60)
wickets = c(2, 3, 4, 5, 6, 7)
df = data.frame(name, place, score, wickets)
sum(df[which(df$name == 'V' & df$place == 'rk'), 3])
Output:
[1] 90
In this article, we calculated the sum of a column in R using both single and multiple conditions.
Similar Reads
Sum of rows in R based on condition To find the sum of rows in R based on a condition, we select only the rows that match the condition and then use the sum() function to add their values.Method 1: Sum of Rows Based on a Single ConditionWe calculate the sum of specific row values filtered by one logical condition.Example: 1team, capta
2 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 vectors1. Using Sum() function to Add two NumbersWe will create a vector a1 with the values 12 and 13, and then calculate the
3 min read
How to Sum Values Based on Criteria in Another Column in Excel? In Excel, we can approach the problem in two ways. [SUMIF Formula and Excel Pivot] Sample data: Sales Report Template We are creating a summary table for Total Product sales. Approach 1: Excel Formula SUMIF Step 1: Copy âColumn Aâ Products and then paste into âColumn Fâ Step 2: Remove duplicate prod
1 min read
Filter Rows Based on Conditions in a DataFrame in R To filter rows in a data frame using R, we can apply conditions directly to the columns. R offers several ways to perform this, depending on whether the condition is single or multiple.1. Filter Rows Based on a Single ConditionThis method filters rows where a specific condition is applied to a singl
2 min read
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. Method 1: Perform a SUMIF Function On One Column: In
3 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