# R Program to find Sum, Mean and Product of a Vector, ignore element like NA or NaN

February 9, 2023, Learn eTutorial
1565

## How to find the Sum, Mean, and Product of a Vector, ignore elements like NA or NaN

Here we are explaining how to write an R program to find the Sum, Mean, and Product of a Vector, ignoring elements like NA or NaN. Here we are using built-in functions sum, mean, prod. The input numbers are directly passed to our functions. The function sum() returns the added value of all the values present in its arguments. The sum of the values divided by 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.

``````sum(…, na.rm = FALSE)
mean(x, …)
prod(…, na.rm = FALSE)
```
```

In the above function argument structure by making na.rm = TRUE we can avoid the elements like NA, NaN.

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 like na.rm = TRUE while calling each function. Finally, print the function result.

## ALGORITHM

STEP 1: Use the built-in functions

STEP 2: Call `sum()` with vector and na.rm = TRUE as argument

STEP 3: Call `mean()` with vector and na.rm = TRUE as argument

STEP 4: Call `prod()` with vector and na.rm = TRUE as argument

STEP 5: Print the result of each function

## R Source Code

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

## OUTPUT

```[1] "Sum is:"
[1] 90
[1] "Mean is:"
[1] 30
[1] "Product is:"
[1] 24000```

VIEW ALL
VIEW ALL