What is sum() Function in R (5 Examples)

The sum() is a built-in R function that calculates the sum of a numeric input vector. The syntax of the sum function is sum(x, na.rm=FALSE), where x is the name of the vector and na.rm is whether to ignore NA values.

Syntax

sum(x, na.rm = FALSE, …)

Parameters

  1. x: numeric vector.
  2. rm: whether NA should be removed; if not, NA will be returned.

Example 1: How to calculate the sum of vector elements in R

A vector in R is the data type to save multiple elements in R.  Let’s find the sum of vectors.

rv <- c(11, 19, 21, 18, 46)

#calculates the sum of the values 
sum(rv)

Output

[1] 115

Example 2: Passing the NA value to the sum() function

If NA is present in the vector elements, then you can’t get the desired output.

rv <- c(11, 19, NA, 18, 46)

#calculates the sum of rv vector
sum(rv)

Output

[1] NA

Well, we did not expect NA output. However, sometimes your dataset may contain ‘NA” values, i.e., ‘Not Available’.

So if you add the values, including NA, the sum() method returns the NA instead of the numerical summation output.

But we can handle this issue by bypassing na.rm = TRUE.

rv <- c(11, 19, NA, 18, 46)

# Calculates the sum of the values 
sum(rv, na.rm = TRUE)

Output

[1] 94

Example 3: Calculating the sum of a data frame in R

To find a sum of the data frame values in R, use the sum() function.  Define a data.frame and execute the sum() function on the column.

student <- data.frame(name = c("Sunny", "Mia", "Kourtney", "Olivia", "Holy"),
                      enrollno = c(11, 18, 19, 29, 46),
                      age = c(32, 31, 32, 27, 30))


print(student)

Output

 RScript Pro.R
   name    enrollno    age
1  Sunny    11         32
2  Mia      18         31
3  Kourtney 19         32
4  Olivia   29         27
5  Holy     46         30

Let’s find the sum of the enrollno column(variable).

 student <- data.frame(name = c("Sunny", "Mia", "Kourtney", "Olivia", "Holy"),
                       enrollno = c(11, 18, 19, 29, 46),
                       age = c(32, 31, 32, 27, 30))

sum(student$enrollno)

To access the column, use $ and then the column name. Here we are finding the sum of enrollno column values. See the below output.

[1] 123

Example 4: Calculating the sum columnwise

To get the sum of specific columns, use the mapply() function. The mapply() function calculates the sum of required columns.

student <- data.frame(name = c("Sunny", "Mia", "Kourtney", "Olivia", "Holy"),
                      enrollno = c(11, 18, 19, 29, 46),
                      age = c(32, 31, 32, 27, 30))

mapply(sum, student[, c(2, 3)])

Output

enrollno age
   123   152

The sum of the enrollno column is 123, and the age is 152. In our data frame, we have two columns with numeric values; that is why it did not give us an error. However, if every column is non-numeric, it will throw an error.

Example 5: Sum of the column bygroup in R

R has a built-in aggregate() function used to find the sum of a column bygroup.

student <- data.frame(name = c("Sunny", "Mia", "Kourtney", "Olivia", "Holy"),
                      enrollno = c(11, 18, 19, 29, 46),
                      age = c(32, 31, 32, 27, 30))

aggregate(x = student$enrollno,
 by = list(student$name),
 FUN = sum) 

Output

   Group.1   x
1  Holy     46
2  Kourtney 19
3  Mia      18
4  Olivia   29
5  Sunny    11

In this example, the aggregate() function and the sum() function compute the sum of a group. Here the sum of the “enrollno” column for “name” is calculated.

Wrapping up

The sum() method in R accepts a numeric vector as an argument and returns the sum of the vector elements.

Leave a Comment