VOOZH about

URL: https://www.geeksforgeeks.org/r-language/sum-of-column-in-r-based-on-condition/

⇱ Sum of column in R based on condition - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

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

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

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

Output:

[1] 90

In this article, we calculated the sum of a column in R using both single and multiple conditions.

Comment

Explore