Here we are explaining how to write an R program to find Sum, Mean, and Product of a Vector, ignore elements like NA or NaN. Here we are using built-in functions sum, mean, prod for this calculation. The numbers are passed to these functions directly here. The function sum() returns the sum of all the values present in its arguments. The sum of the values dividing with the number of values in a data series is calculated using the mean() function. Finally, the prod() is for finding the product of given arguments.
In the above function argument structure by making na.rm = TRUE we can avoid the elements like NA, NAN.
Given below are the steps which are used in the R program to find the sum, mean, and product of the vector values. In this R program, we directly give the values to built-in functions. Consider variable A for assigning vector value. And call each function by giving A as an argument. Make sure na.rm should be true as like na.rm = TRUE. Finally, print the function result.
A = c(30, NULL, 40, 20, NA)
print("Sum is:")
## [1] "Sum is:"
print(sum(A, na.rm=TRUE))
## [1] 90
print("Mean is:")
## [1] "Mean is:"
print(mean(A, na.rm=TRUE))
## [1] 30
print("Product is:")
## [1] "Product is:"
print(prod(A, na.rm=TRUE))
## [1] 24000
A = c(30, 55, 40, 20, 127)
print("Sum is:")
## [1] "Sum is:"
print(sum(A, na.rm=TRUE))
## [1] 272
print("Mean is:")
## [1] "Mean is:"
print(mean(A, na.rm=TRUE))
## [1] 54.4
print("Product is:")
## [1] "Product is:"
print(prod(A, na.rm=TRUE))
## [1] 167640000