# Get Sum of Data Frame Column Values in R (2 Examples)

In this article youâ€™ll learn how to **compute the sum of one or all columns of a data frame** in the R programming language.

Table of contents:

Letâ€™s do this:

## Introducing Example Data

As a first step, weâ€™ll have to create some data that we can use in the examples later on:

data <- data.frame(x1 = 1:5, # Create example data x2 = 2:6, x3 = 3) data # Return example data # x1 x2 x3 # 1 1 2 3 # 2 2 3 3 # 3 3 4 3 # 4 4 5 3 # 5 5 6 3

The previous output of the RStudio console shows that our example data has five rows and three columns. Each of the three variables is numeric.

## Example 1: Compute Sum of One Column Using sum() Function

In Example 1, Iâ€™ll explain how to return the sum of only one variable of our data frame (i.e. x1). For this, we can use the sum function as shown below:

sum(data$x1) # Applying sum function # 15

The sum of all values contained in the column x1 is 15.

## Example 2: Compute Sum of All Columns Using colSums() Function

We can also compute the sum of all numeric columns of our data frame. The following R code explains how to do this using the colSums function in R.

colSums(data) # Applying colSums function # x1 x2 x3 # 15 20 15

The output of the colsums function illustrates the column sums of all variables in our data frame.

## Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. In the video, Iâ€™m showing the R programming code of this article:

In addition, you could read the related articles on my website. You can find a selection of articles about descriptive statistics below.

- sum Function in R
- colSums, rowSums, colMeans & rowMeans in R
- cumsum Function in R
- R Programming Language

In this R programming post you learned how to **calculate sums of variables**. In case you have any further questions, please let me know in the comments section below.

## 4 Comments. Leave new

Hi !

I have some trouble making a sum of my data. I’m trying to sum only one of my columns so I’ve tried the sum(data$column) function but it replies NA.

Same with colsums. I’ve tried seperating my column into its own dataframe. It doesn’t give an error but it returns NA.

My field is numerical (i checked with is.numeric) so i’m a bit confused about what is happening here.

Thanks

Emma

Hello Emma,

It is probably due to the fact that your data has missing values. Please use the argument na.rm=TRUE. See the example code:

Best,

Cansu

Oh yes that’s it ! I thought it could get around the missing values…

Many thanks,

Emma

Hello Emma,

Great to know it helped!

Best,

Cansu