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?



Follow us on Facebook and Twitter for latest update.