Calculate Geometric Mean in R

Calculate Geometric Mean in R, Geometric mean is the nth root of the product of n values of a set of observations.

Geometric can be expressed as

GM=(x1, x2, x3, ……, xn)1/n

The advantage of the geometric mean is

  • It is least affected by extreme values
  • It is based on all observations of the set
  • It is suitable for further algebraic treatment.

The disadvantageous of the geometric mean is

  • Calculation is complicated
  • Cannot be calculated if a set of value contains zero If any one or more values are negative, either geometric mean will not be calculated or an absurd value will be obtained.

Decision Trees in R » Classification & Regression »

Calculate Geometric Mean in R

The syntax is as follows,


Approach 1: GM of Vector

Let’s create a vector for geometric mean calculation.

data <- c(1, 15, 12, 5, 18, 11, 12, 15, 18, 25)

Log Rank Test in R-Survival Curve Comparison »

Approach 2: GM Vector with Zeros

As mentioned earlier if a set of values contains zero cannot be executed a geometric mean, However, we can ignore zero values and execute GM.

data <- c(1, 15, 12, 5, 0, 18, 11, 12, 15, 18, 25, 0, -11)

The above data set included negative values and zero.

Let’s make use of the below code for GM calculation.


Kruskal Wallis test in R-One-way ANOVA Alternative »

Approach 3: GM Data Frame

Let’s create a data frame for geometric mean calculation.

data<- data.frame(x=c(10, 13, 14, 26, 38, 28, 29),
                 y=c(15, 8, 18, 17, 1, 1, 6),
                 z=c(12, 10, 18, 28, 29, 29, 12))

Calculate the geometric mean for x, y, and z.

apply(data[ , c('x', 'y', 'z')], 2, function(x) exp(mean(log(x))))
    x         y         z
20.379699  5.798203 17.992195


Geometric Mean is considered the most suitable average for index numbers.

Principal component analysis (PCA) in R »

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

4 × 4 =