How to Calculate Geometric Mean in R?
Last Updated :
23 Jul, 2025
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:
[1] 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.packages('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:
[1] 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:
[1] 2.605171
[1] 26.67781
[1] 65.54881
Similar Reads
How to calculate mean of a CSV file in R? Mean or average is a method to study central tendency of any given numeric data. It can be found using the formula. Mean= (sum of data)/(frequency of data) In this article, we will be discussing two different ways to calculate the mean of a CSV file in R. Data in use: Method 1: Using mean function I
2 min read
How to Calculate a Trimmed Mean in R? In this article, we will discuss how to calculate trimmed mean in R Programming Language. A trimmed mean is the mean of the given data that is calculated after removing a specific percentage of the smallest and largest number from the given data. Example: Given a set of elements- [3,4,5,2,8,7,6,9,10
2 min read
How to Calculate Expected Value in R? In this article, we are going to see how to calculate the excepted value using R Programming Language. A probability distribution describes all the possible values of random variables in the given range.Expected value of a probability distribution:μ = Σx * P(x)Where X is a Sample value and P(x) is a
2 min read
How to Calculate the Standard Error of the Mean in R? In this article, we will discuss how to calculate the standard error of the mean in the R programming language. StandardError Of Mean is the standard deviation divided by the square root of the sample size. Formula: Standard Error: (Sample Standard Deviation of Sample)/(Square Root of the sample siz
2 min read
How to Calculate Mean, Median and Mode in Excel Understanding the concepts of mean, median and mode in Excel can grow your mathematics and Excel skills. Understanding how to calculate mean in Excel, work out mean on Excel, how to calculate median in Excel, find median in Excel, and calculate mode in Excel can revolutionize how you interpret data.
9 min read
How To Calculate Cumulative Sum By Group In R The sum of a collection of numbers as the sum value increases with the number sequence is known as the cumulative sum. In data analysis tasks, it is essential to calculate cumulative sums within groups. This operation helps when we deal with time series or categorical data. In this article, we will
5 min read