Find Sum, Mean and Product of Vector in R Programming
In this example, you will learn to find sum, mean and product of vector elements using built-in functions.
To understand this example, you should have the knowledge of following R programming topics:
We can sum the elements of a vector using the
prod() functions can be used to find the mean and product of the terms.
Example: Vector Elements Arithmetic
> sum(2,7,5)  14 > x  2 NA 3 1 4 > sum(x) # if any element is NA or NaN, result is NA or NaN  NA > sum(x, na.rm=TRUE) # this way we can ignore NA and NaN values  10 > mean(x, na.rm=TRUE)  2.5 > prod(x, na.rm=TRUE)  24
Whenever a vector contains
NA (Not Available) or
NaN (Not a Number), functions such as
prod() etc. produce
In order to ignore such values, we pass in the argument
na.rm = TRUE.