VOOZH about

URL: https://www.geeksforgeeks.org/r-language/find-sum-mean-and-product-of-a-vector-in-r/

⇱ Find Sum, Mean and Product of a Vector in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find Sum, Mean and Product of a Vector in R

Last Updated : 26 Mar, 2021

sum(), mean(), and prod() methods are available in R which are used to compute the specified operation over the arguments specified in the method. In case, a single vector is specified, then the operation is performed over individual elements, which is equivalent to the application of for loop. 

Function Used:

  • mean() function is used to calculate mean

Syntax: mean(x, na.rm)

Parameters:

  • x: Numeric Vector
  • na.rm: Boolean value to ignore NA value
  • sum() is used to calculate sum

Syntax: sum(x)

Parameters:

  • x: Numeric Vector
  • prod() is used to calculate product

Syntax: prod(x)

Parameters:

  • x: Numeric Vector

Given below are examples to help you understand better.

Example 1:

Output

[1] "Sum of the vector:"

[1] 10

[1] "Mean of the vector:"

[1] 2.5

[1] "Product of the vector:"

[1] 24

Example 2:

Output

[1] "Sum of the vector:"

[1] 6.1

[1] "Mean of the vector:"

[1] 2.033333

[1] "Product of the vector:"

[1] 6.6

Example 3 : Vector with NaN values 

Output

[1] "Sum of the vector:"

[1] NA

[1] "Mean of the vector with NaN values:"

[1] NA

[1] "Mean of the vector without NaN values:"

[1] 2.033333

[1] "Product of the vector:"

[1] NA

Comment

Explore