Sum in R: How to Find Sum of Vectors

The sum() method helps find the sum of a group, the sum of a specific column of a data frame. The sum() method returns the sum of all the elements present in its arguments. If it is a vector, then it will return the sum of all vector elements.

Sum in R

The sum() is a built-in R function that calculates the sum of a numeric input vector. It accepts a numeric vector as an argument and returns the sum of the vector elements. To calculate the sum of vectors in R, use the sum() function.

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 of sum function in R

Let’s start with a vector. 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)

Here, we defined a vector and used the sum() method to calculate the sum of all the vector elements.

See the output.

[1] 115

Passing 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

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

Calculate the sum columnwise

If you want 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, then it will throw an error.

Sum of the column bygroup in R

R has a built-in aggregate() function is 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 along with the sum() function computes the sum of a group. Here the sum of the “enrollno” column for “name” is calculated.

Wrapping up

R sum() method is used to find the sum of the elements in the vector. This guide showed you how to find the sum of the vectors, data frame, and its different variants.

That’s it for this tutorial.

Leave a Comment