Home » How to Sum Specific Rows in R (With Examples)

How to Sum Specific Rows in R (With Examples)

by Tutor Aspire

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

You may also like