8 C
London
Tuesday, March 11, 2025
HomeStatistics TutorialRHow to Sum Specific Rows in R (With Examples)

How to Sum Specific Rows in R (With Examples)

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...

We can use the following syntax to sum specific rows of a data frame in R:

with(df, sum(column_1[column_2 == 'some value']))

This syntax finds the sum of the rows in column 1 in which column 2 is equal to some value, where the data frame is called df.

This tutorial provides several examples of how to use this function in practice with the following data frame:

#create data frame
df 
                 points = c(4, 7, 8, 8, 8, 9, 12),
                 rebounds = c(3, 3, 4, 4, 6, 7, 7))

#view data frame
df

  team points rebounds
1    A      4        3
2    A      7        3
3    B      8        4
4    B      8        4
5    B      8        6
6    C      9        7
7    C     12        7

Example 1: Sum Rows Based on the Value of One Column

The following code shows how to find the sum of all rows in the points column where team is equal to C:

#find sum of points where team is equal to 'C'
with(df, sum(points[team == 'C']))

[1] 21

And the following code shows how to find the sum of all rows in the rebounds column where the value in the points column is greater than 7:

#find sum of rebounds where points is greater than 7
with(df, sum(rebounds[points > 7]))

[1] 28

Example 2: Sum Rows Based on the Value of Multiple Columns

The following code shows how to find the sum of the rows in the rebounds column where the value in the points column is less than 8 or the value in the team column is equal to C:

with(df, sum(rebounds[points | team == 'C']))

[1] 20

And the following code shows how to find the sum of the rows in the rebounds column where the value in the points column is less than 10 and the value in the team column is equal to B:

with(df, sum(rebounds[points & team == 'B']))

[1] 14

Additional Resources

How to Arrange Rows in R
How to Remove Duplicate Rows in R
How to Remove Rows with Some or All NAs 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