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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework