11 C
London
Saturday, May 10, 2025
HomeRDescriptive Statistics in RHow to Sum Columns Based on a Condition in R

How to Sum Columns Based on a Condition in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to sum columns based on condition in R:

#sum values in column 3 where col1 is equal to 'A'
sum(df[which(df$col1=='A'), 3])

The following examples show how to use this syntax in practice with the following data frame:

#create data frame
df frame(conference = c('East', 'East', 'East', 'West', 'West', 'East'),
                 team = c('A', 'A', 'A', 'B', 'B', 'C'),
                 points = c(11, 8, 10, 6, 6, 5),
                 rebounds = c(7, 7, 6, 9, 12, 8))

#view data frame
df

  conference team points rebounds
1       East    A     11        7
2       East    A      8        7
3       East    A     10        6
4       West    B      6        9
5       West    B      6       12
6       East    C      5        8

Example 1: Sum One Column Based on One Condition

The following code shows how to find the sum of the points column for the rows where team is equal to ‘A’:

#sum values in column 3 (points column) where team is equal to 'A'
sum(df[which(df$team=='A'), 3])

[1] 29

The following code shows how to find the sum of the rebounds column for the rows where points is greater than 9:

#sum values in column 4 (rebounds column) where points is greater than 9
sum(df[which(df$points > 9), 4])

[1] 13

Example 2: Sum One Column Based on Multiple Conditions

The following code shows how to find the sum of the points column for the rows where team is equal to ‘A’ and conference is equal to ‘East’:

#sum values in column 3 (points column) where team is 'A' and conference is 'East'
sum(df[which(df$team=='A' & df$conference=='East'), 3])

[1] 29

Note that the & operator stands for “and” in R.

Example 3: Sum One Column Based on One of Several Conditions

The following code shows how to find the sum of the points column for the rows where team is equal to ‘A’ or ‘C’:

#sum values in column 3 (points column) where team is 'A' or 'C'
sum(df[which(df$team == 'A' | df$team =='C'), 3])

[1] 34

Note that the | operator stands for “or” in R.

Additional Resources

The following tutorials explain how to perform other common functions in R:

How to Sum Specific Columns in R
How to Sum Specific Rows in R
How to Calculate Sum by Group in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories