sum Function in R (3 Examples)
In this tutorial you’ll learn how to compute the sum in R.
The article is mainly based on the sum() function. Let’s have a look at the basic R syntax and the definition of the sum function:
Basic R Syntax of sum():
sum(x)
Definition of sum():
The sum R function computes the sum of a numeric input vector.
In this tutorial I’ll explain in three examples how to apply the sum function in R.
Let’s jump right to it.
Example 1: Basic Application of sum() in R
First, we need to create some example data to which we can apply the sum R function. Consider the following numeric vector:
x <- c(10, 5, 8, 3, 5) # Create example vector
To this vector, we can now apply the sum function as follows:
sum(x) # Sum of example vector # 31
You can see based on the output of the RStudio console that the summation of our example vector is 31.
That was easy, but however, sometimes there might occur problems when the sum function is used. So keep on reading…
Example 2: Handle NA Values with sum Function
A typical problem, when descriptive statistics such as the sum are computed, is the occurrence of NA values (i.e. missing data).
Let’s create another example vector, which contains NA values:
x_NA <- c(x, NA) # Create example vector with NA x_NA # Print example vector # 10 5 8 3 5 NA
If we now apply the sum function to this vector, the RStudio console returns NA instead of a numeric value:
sum(x_NA) # Try to get sum of NA vector # NA
Fortunately, the sum function provides an easy solution for this problem. We simply have to specify the option na.rm = TRUE:
sum(x_NA, na.rm = TRUE) # Specify na.rm argument # 31
As in Example 1, the returned result is 31 – Looks good.
Example 3: Compute Sum of Data Frame Column
So far, we have only applied the sum command to oversimplified synthetic data. In this example, I’m therefore showing you how to calculate the sum of the column of a real data set.
For the example, I’m going to use the Iris Flower data. Let’s load and inspect the data in R:
data(iris) # Load iris data head(iris) # Head of iris data
Table 1: First Six Rows of the Iris Flower Data Matrix.
Let’s assume that we want to do an addition of all values in the first column Sepal.Length. Then we can use the following R code:
sum(iris$Sepal.Length) # Sum of first column of iris # 876.5
If we add all values of the first column, we get the result 876.5.
Video, Further Resources & Summary
On my YouTube channel, you can find a video that explains the examples of this tutorial in more detail. You can check out the video below:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you could have a look at some of the other R tutorials of my website:
- Sum by Group in R
- Weighted Sum in R
- Column & Row Sums & Means
- The cumsum Function in R
- R Functions List (+ Examples)
- The R Programming Language
This tutorial explained how to get the sum of a numeric data object in the R programming language. In case you have any further questions on the usage of the sum function, please let me know in the comments.
Statistics Globe Newsletter