# Harmonic Mean in R (2 Examples)

This page shows how to compute the harmonic mean in the R programming language.

I will show based on two reproducible examples how to calculate the harmonic mean for a numeric vector or a data frame column.

You are here for the answer, so let’s dig in!

## Example 1: Compute Harmonic Mean of Vector in R

In the first example, I’ll explain how to calculate the harmonic mean of a numeric vector. So let’s create such a numeric vector first:

`x <- c(10, 14, 29, 30, 41, 53, 27, 55) # Create example vector`

In order to get the harmonic mean of this vector, we need to install and load the psych add-on package:

```install.packages("psych") # Install psych package library("psych") # Load psych package```

Now, we can apply the harmonic.mean function of the psych package to compute the harmonic mean of our example vector:

```harmonic.mean(x) # Apply harmonic.mean function # 23.68814```

As you can see based on the RStudio console output, the harmonic mean of our example vector is 23.68814.

Note: The harmonic.mean command could also be applied to data with NA values (i.e. missing values). By default, such values are removed before processing.

## Example 2: Harmonic Mean of Data Frame Column

In the second example, we will apply the harmonic.mean R function of the psych package to some real data, i.e. the Iris Flower data set. We can load the data as follows:

```data(iris) # Load iris data head(iris) # First rows of iris data``` Table 1: The First Six Rows of the Iris Flower Data Matrix.

Now, we use the harmonic.mean to compute, for instance, the harmonic mean of the first data frame column:

```harmonic.mean(iris\$Sepal.Length) # Apply harmonic.mean to column # 5.728905```

The harmonic mean of the first column is 5.728905.

## Further Resources & Summary

After reading this article, you should be able to calculate harmonic means in R. However, we didn’t speak about the theoretical research concept of the harmonic mean yet. In case you want to learn more about the statistical concept behind the harmonic mean, you could have a look at the following YouTube video of zedstatistics. The speaker explains the concept of the harmonic mean and compares it with other measures, which compute the average, such as the arithmetic mean or the geometric mean.

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, you might have a look at some of the other R programming tutorials that I have published on statisticsglobe.com:

This is all I wanted to show you in this tutorial. However, in case you have any questions or comments please let me know in the comments section below.

Subscribe to my free statistics newsletter