Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

harmonic.mean

Find the harmonic mean of a vector, matrix, or columns of a data.frame


Description

The harmonic mean is merely the reciprocal of the arithmetic mean of the reciprocals.

Usage

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

Arguments

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

Details

Included as an example for teaching about functions. As well as for a discussion of how to estimate central tendencies. Also used in statsBy to weight by the harmonic mean.

Values of 0 can be included (in which case the harmonic.mean = 0) or converted to NA according to the zero option.

Added the zero option, March, 2017.

Value

The harmonic mean(s)

Note

Included as a simple demonstration of how to write a function

Examples

x <- seq(1,5)
x2 <- x^2
x2[2] <- NA
y <- x - 1
X <- data.frame(x,x2,y)
harmonic.mean(x)
harmonic.mean(x2)
harmonic.mean(X)
harmonic.mean(X,na.rm=FALSE)
harmonic.mean(X,zero=FALSE)

psych

Procedures for Psychological, Psychometric, and Personality Research

v2.1.3
GPL (>= 2)
Authors
William Revelle [aut, cre] (<https://orcid.org/0000-0003-4880-9610>)
Initial release
2021-03-21

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.