This article explains exactly how to compute the geometric mean in the R programming language.

You are watching: Geometric mean in r

This short article contains two examples consisting of reproducible R codes.

So without additional ado, let’s relocate on come the examples!

Example Data

In the examples of this tutorial, I’ll usage the adhering to numeric vector together exemplifying data:

exp(mean(log(x))) # Compute geometric mean manually# 4.209156

exp(mean(log(x))) # Compute geometric mean manually# 4.209156

As you deserve to see, the geometric typical of our instance data is 4.209156.

This seems to be too complicated to carry out it on a constant basis? No problem! The following instance makes the computation of the geometric mean also easier…

Example 2: geometric.mean duty of psych R Package

The psych package is an R add-on parcel which, besides plenty of other very useful functions, consists of the geometric.mean command. Let’s see how we have the right to use the geometric.mean function in practice.

First, we need to install and load the psych package to R…

install.packages(\"psych\") # download psych packagelibrary(\"psych\") # load psych package

install.packages(\"psych\") # install psych packagelibrary(\"psych\") # pack psych package

…and then we can apply the geometric.mean duty to compute the geometric median of our example data:

geometric.mean(x) # use geometric.mean function# 4.209156

geometric.mean(x) # apply geometric.mean function# 4.209156

The result is 4.209156 – the exact same value we gained with our hand-operated solution of example 1.

By the way: The geometric.mean function also provides an option to eliminate NA worths from your computation that the geometric mean. Definitely an additional advantage the the psych package compared to the hand-operated solution.

See more: When Actual Inflation Is Less Than Expected Inflation (Article)


August 2, 2021 12:48 am



Get constant updates ~ above the latest tutorials, uses & news at Statistics Globe. I dislike spam & you might opt the end anytime: Privacy Policy.