How to find Sum, Mean and Product of a Vector in R

 

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:

 

vec = c(1, 2, 3 , 4)

print("Sum of the vector:")

  

# inbuilt sum method 

print(sum(vec))

  

# using inbuilt mean method

print("Mean of the vector:")

print(mean(vec))  

  

# using inbuilt product method

print("Product of the vector:")

print(prod(vec))

 

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:

vec = c(1.1, 2, 3.0 )

print("Sum of the vector:")

  

# inbuilt sum method 

print(sum(vec))

  

# using inbuilt mean method

print("Mean of the vector:")

print(mean(vec))  

  

# using inbuilt product method

print("Product of the vector:")

print(prod(vec))

 

 

 

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 

# declaring a vector

vec = c(1.1,NA, 2, 3.0,NA )

print("Sum of the vector:")

  

# inbuilt sum method 

print(sum(vec))

  

# using inbuilt mean method

print("Mean of the vector with NaN values:")

  

# not ignoring NaN values

print(mean(vec))  

  

# ignoring missing values

print("Mean of the vector without NaN values:")

print(mean(vec,na.rm = TRUE))  

  

# using inbuilt product method

print("Product of the vector:")

print(prod(vec))

 

 

 

 

 

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

 

Find product of vector elements in R

Method 1: Using iteration

Approach

·         Create dataframe

·         Iterate through the vector

·         Multiply elements as we go

·         Display product

The following code snippet indicates the application of for loop over decimal point vector. The product obtained is also of the decimal type. 

Example:

 

# declaring a floating point vector 

vec <- c(1.1,2,3.2,4)

size = length(vec)

  

# initializing product as 1

prod = 1

  

# looping over the vector elements

for(i in 1:size)

 {

  # multiplying the vector element at ith index 

  # with the product till now

  prod = vec[i]*prod

 }

print("Product of vector elements:")

  

# in-built application of prod function

print(prod)

 

Output

[1] “Product of vector elements:”

[1] 28.16

 

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction