sum

Basics

sum is a function that calculates the sum of a vector of values.


Examples

How do I get the sum of the values in a vector?

Click to see solution
sum(c(1,3,2,10,4))
[1] 20

How do I get a vector’s sum when some of the values are: NA, NaN?

Click to see solution

See our mean page for information on na.rm.

sum(c(1,2,3,NaN), na.rm=TRUE)
[1] 6
sum(c(1,2,3,NA), na.rm=TRUE)
[1] 6
sum(c(1,2,NA,NaN,4), na.rm=TRUE)
[1] 7