How can I sum specific rows in R, with examples?

In R, users can sum specific rows within a data frame by using the “rowSums” function. This function allows for the selection of specific rows within a data frame and calculates the sum of the values in those rows. For example, to sum the first and third rows of a data frame named “df”, the code would be: “rowSums(df[c(1,3),])”. This command would return a vector with the sum of values in the first and third rows. This function is useful for data analysis and can be applied to any numeric data frame in R.

Sum Specific Rows in R (With Examples)


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

Additional Resources

x