In this tutorial, you will learn about what is harmonic mean, when harmonic mean is used and how to calculate harmonic mean in R.

## What is harmonic mean?

Harmonic mean is a measure of central tendency. It is useful in finding the averages involving time, rate, price and ratio.

Let $x_i, i=1,2, \cdots , n$ be $n$ positive observations then the harmonic mean of $X$ is denoted by $HM$ and is defined as

`$$HM = \dfrac{n}{\sum_{i=1}^{n} \dfrac{1}{x_i}}$$`

Thus, the harmonic mean is the reciprocal of the arithmetic mean of the reciprocal of the observations.

## When to use harmonic mean?

Harmonic mean is often used in averaging the data about rates and ratios. Harmonic mean is commonly used in finance to calculate average price to earning ratio, average speed.

## How to calculate Harmonic mean in R?

As such there is no specific function available in `base`

package. But you can define your own function for harmonic mean or use `harmonic.mean()`

function from `psych`

package.

### Harmonic mean using user-defined function

Let us define our own function to calculate harmonic mean as follows:

```
Harmonic.Mean <- function(x, na.rm = TRUE) {
if (na.rm == FALSE & any(is.na(x))) {
avg <- NA
} else{
avg <- 1 / mean(1 / x, na.rm = TRUE)
}
return(avg)
}
```

### Using `harmonic.mean()`

function from `psych`

package

One can use the `harmonic.mean()`

function from `psych`

package. First you need to install the `psych`

package using `install.packages("psych")`

. Once the package is installed, then load the package using `library(psych)`

.

The syntax of the `harmonic.mean()`

function is

`harmonic.mean(x,na.rm=TRUE,zero=TRUE)`

where,

**x :**a vector, matrix or data.frame,**na.rm :**`na.rm=TRUE`

remove`NA`

values before processing,**zero :**if`TRUE`

, then if there are any zeros, return 0, else, return the harmonic mean of the non-zero elements

For more information about `psych`

package check this link psych package.

Note that, the harmonic mean is not useful if there are elements that are zero.

## Numerical example on harmonic mean using R

### Example 1: Harmonic Mean Using R

A bus driver travels total 30 miles. He travel first 10 miles with speed of 30 miles per hour, second 10 miles with the speed of 35 miles per hour and last 10 miles with the speed of 45 miles per hour. What is the average speed?

#### Harmonic mean using formula

The harmonic mean is the reciprocal of the mean of reciprocal of the observations. That is

`$$HM = \dfrac{1}{\text{mean(1/x)}}$$`

```
# create a data vector
x <- c(30, 35, 45)
# calculate harmonic mean
HM <- 1/mean(1/x)
HM
```

`[1] 35.66038`

The average speed is 35.6603774 miles per hour.

#### Harmonic mean using user-defined function

Earlier we have defined the function for harmonic mean as `Harmonic.Mean(x)`

. Let us calculate the harmonic mean using user-defined function `Harmonic.Mean(x)`

.

```
# create a data vector
x <- c(30, 35, 45)
Harmonic.Mean(x)
```

`[1] 35.66038`

The average speed is 35.6603774 miles per hour.

#### Harmonic mean using `psych`

package

One can use `harmonic.mean()`

function from `psych`

package to calculate harmonic mean.

```
# load the package
library(psych)
```

`Warning: package 'psych' was built under R version 4.0.5`

```
# use the function to compute harmonic mean
harmonic.mean(x)
```

`[1] 35.66038`

The average speed is 35.6603774 miles per hour.

### Example 2: Harmonic Mean using R

Compute the harmonic mean of data

`10,25,36,23,NA,17`

.

#### Harmonic mean using formula

The harmonic mean is the reciprocal of the mean of reciprocal of the observations. That is

`$$HM = \dfrac{1}{\text{mean(1/x)}}$$`

```
# create a data vector
x <- c(10,25,36,23,NA,17)
# calculate harmonic mean
HM <- 1/mean(1/x,na.rm=TRUE)
HM
```

`[1] 18.51306`

The harmonic mean of given data is 18.5130628.

Note that the default value of `na.rm`

is `FALSE`

in `mean()`

function. Therefore, to remove `NA`

values before processing use the argument `na.rm=TRUE`

.

#### Harmonic mean using user-defined function

Use the user-defined function `Harmonic.Mean(x)`

with additional argument `na.rm=TRUE`

.

```
# create a data vector
x <- c(10,25,36,23,NA,17)
Harmonic.Mean(x)
```

`[1] 18.51306`

The harmonic mean of given data is 18.5130628.

Note that the default value of `na.rm`

is `TRUE`

in our user-defined function `Harmonic.Mean()`

function. Therefore, no need to specify the argument `na.rm=TRUE`

.

#### Harmonic mean using `psych`

package

Use `harmonic.mean()`

function from `psych`

package to calculate harmonic mean.

```
# load the package
library(psych)
# use the function to compute harmonic mean
harmonic.mean(x)
```

`[1] 18.51306`

The harmonic mean of given data is 18.5130628.

Note that the default value of `na.rm`

is `TRUE`

in `harmonic.mean()`

function from `psych`

package. Therefore, the `NA`

values are removed before processing.

## Endnote

In this tutorial, you learned about what is harmonic mean and how to compute harmonic mean for the data using R.

To learn more about descriptive statistics using R, please refer to the following tutorials:

Geometric mean using R

Descriptive Statistics using R

Hopefully you enjoyed this tutorial on harmonic mean using R.