Open In App

Sum of column in R based on condition

Last Updated : 21 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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