Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

R Programming: Find Sum, Mean and Product of a Vector, ignore element like NA or NaN

R Programming: Vector Exercise-7 with Solution

Write a R program to find Sum, Mean and Product of a Vector, ignore element like NA or NaN.

Sample Solution :

R Programming Code :

x = c(10, NULL, 20, 30, NA)
print("Sum:")
#ignore NA and NaN values
print(sum(x, na.rm=TRUE))
print("Mean:")
print(mean(x, na.rm=TRUE))  
print("Product:")
print(prod(x, na.rm=TRUE))

Sample Output:

[1] "Sum:"
[1] 60
[1] "Mean:"
[1] 20
[1] "Product:"
[1] 6000                         

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to find Sum, Mean and Product of a Vector.
Next: Write a R program to find the minimum and the maximum of a Vector.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?