Skip to Content

Sum of Values in an R Column

The R programming language comes with a number of helpful functions to work with the data stored in data structures like vectors, lists, dataframes, etc. In this tutorial, we will look at one such function that helps us get the sum of the values in a column of an R dataframe.

How to get the sum of values in an R dataframe column?

sum of values in an R column

You can use the built-in sum() function in R to compute the sum of values in a dataframe column. Pass the column values as an argument to the function.

The following is the syntax –

sum(dataframe[[column_name]])

Pass na.rm=TRUE to avoid the NA values when computing the sum.

It returns the sum of the values in the passed column.

Steps to compute the sum of values in an R column

Let’s now look at a step-by-step example of using the above syntax to compute the sum of a numeric column in R.

Step 1 – Create a dataframe

First, we will create an R dataframe that we will be using throughout this tutorial.

# create a dataframe
scores_df = data.frame(
  "Team_A"= c(70, 80, 90),
  "Team_B"= c(65, 95, 91)
)
# display the dataframe
print(scores_df)

Output:

  Team_A Team_B
1     70     65
2     80     95
3     90     91

We now have a dataframe containing the scores of two teams during a three-match basketball tournament. The dataframe has the columns – “Team_A” and “Team_B”. The values in each column represent the scores by the respective team.

Step 2 – Calculate the sum of values in the column using the sum() function

To calculate the sum of values in a column, pass the column values as an argument to the sum() function. You can use the [[]] notation to access the values of a column.

Let’s compute the total points scored by both teams.

# sum of values in "Team_A"
total_A = sum(scores_df[["Team_A"]])
# sum of values in "Team_B"
total_B = sum(scores_df[["Team_B"]])
# display the totals
print(total_A)
print(total_B)

Output:

[1] 240
[1] 251

We get the sum of values in the “Team_A” column and the “Team_B” column.

Sum of a column with NA values in R

What if there are NA values in a column?

Let’s find out.

# create a dataframe
scores_df = data.frame(
  "Team_A"= c(70, NA, 90),
  "Team_B"= c(65, 95, 91)
)
# display the dataframe
print(scores_df)

Output:

  Team_A Team_B
1     70     65
2     NA     95
3     90     91

Here, we created a new dataframe such that one column contains NA and the other doesn’t contain any NA values.

Now, let’s apply the sum() function to both columns and compare the results.

# sum of values in "Team_A"
total_A = sum(scores_df[["Team_A"]])
# sum of values in "Team_B"
total_B = sum(scores_df[["Team_B"]])
# display the totals
print(total_A)
print(total_B)

Output:

[1] NA
[1] 251

We get NA as the sum for the column with NA values and the actual sum for the column without NA values. This happened because performing any mathematical operation with NA results in an NA in R.

If you want to compute the sum of a column with NA values, pass na.rm=TRUE to the sum() function to skip the NA values when computing the sum.

# sum of values in "Team_A"
total_A = sum(scores_df[["Team_A"]], na.rm=TRUE)
# sum of values in "Team_B"
total_B = sum(scores_df[["Team_B"]], na.rm=TRUE)
# display the totals
print(total_A)
print(total_B)

Output:

[1] 160
[1] 251

We now get the sum for the “Team_A” column excluding the NA values.

Summary – Sum of Column Values in R

In this tutorial, we looked at how to compute the sum of values in a column of an R dataframe. The following is a short summary of the steps –

  1. Create a dataframe (skip this step if you already have a dataframe on which you want to operate).
  2. Use the sum() function to compute the sum of column values.
  3. If your column contains any NA values, pass na.rm=TRUE to the sum() function to compute the sum excluding the NA values in the column.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Tags

Tags