How to Sum Specific Rows in R (With Examples)

In R, you can sum specific rows by using the rowSums() function. You can specify which rows to sum by including a vector of row numbers or logical conditions to the function. The rowSums() function will then return a vector with the sum of the specified rows. This can be useful for quickly summarizing the data in a data frame or matrix. Examples of how to use the rowSums() function in R are included to help you better understand how it works.


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 <- data.frame(team = c('A', 'A', 'B', 'B', 'B', 'C', 'C'),
                 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 < 8 | 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 < 10 & team == 'B']))

[1] 14

x