# How to Calculate Geometric Mean in R?

• Last Updated : 19 Dec, 2021

In this article, we will discuss how to calculate the Geometric Mean in R Programming Language.

We can define the geometric mean as the average rate of return of a set of values calculated using the products of the terms.

## Method 1: Compute Geometric Mean Manually

In this method, the user can calculate manually by using exp(), mean() and log() functions together by passing the parameter as the given data to calculate the geometric mean in the R programming language.

• exp() function is used to get the exponent from mean data.
• mean() is used to get the mean from log data
• log() is used to get the log value.

Syntax:

`exp(mean(log(data))) `

Example:

In this example, we are going to calculate the geometric mean of 5 elements in a vector using the exp(), mean() and log() functions together in R language.

## R

 `# create  vector``data=``c``(1,2,3,4,5)`` ` `# calculate geometric mean``exp``(``mean``(``log``(data)))`

Output:

` 2.605171`

## Method 2: Using geometric.mean Function of psych Package

In this approach, the user has to first install and import the psych package in the working R console, then the user has to call the geometric.mean() function with the required parameter passed into it to calculate the geometric mean of the given data.

Syntax to install and import the psych package:

```install.package('psych')
library('psych')```

Syntax:

`geometric.mean(data)`

Example:

In this example, we will be calculating the geometric mean of 5 elements in the vector using the geometric.mean() function from the psych package in the R programming language.

## R

 `# load the library``library``(psych)`` ` `# create  vector``data=``c``(1,2,3,4,5)`` ` `# calculate geometric mean``geometric.mean``(data)`

Output:

` 2.605171`

## Method 3: Calculate Geometric Mean of Columns in Data Frame

In this method to calculate the geometric mean of the columns of the given data frame, the user needs to first install and import the psych package in the R console and then call the geometric.mean() function of this package and pass the column name of the dataframe with \$ operator to which the geometric mean id to be calculated in the R programming language.

Syntax:

`geometric.mean(data\$column_name)`

where,

• data is the input dataframe
• column_name is the column of the dataframe

Example:

Under this example, we will be calculating the geometric mean of all the columns of the given data frame of 3 columns and 5 rows using the geometric.mean() function from psych package in the R language.

## R

 `# load the library``library``(psych)`` ` `# create  dataframe with 3 columns``data=``data.frame``(col1=``c``(1,2,3,4,5),``                ``col2=``c``(23,45,32,12,34),``                ``col3=``c``(34,78,90,78,65))`` ` `# calculate geometric mean for column1``geometric.mean``(data\$col1)`` ` `# calculate geometric mean for column2``geometric.mean``(data\$col2)`` ` `# calculate geometric mean for column3``geometric.mean``(data\$col3)`

Output:

``` 2.605171
 26.67781
 65.54881```

My Personal Notes arrow_drop_up