iemisc (version 0.9.8)

shm: Harmonic mean

Description

This function computes the sample harmonic mean.

Usage

shm(x, na.rm = FALSE)

Arguments

x

numeric vector that contains the sample data points.

na.rm

logical vector that determines whether the missing values should be removed or not.

Value

sample harmonic mean as a numeric vector. The default choice is that any NA values will be kept (na.rm = FALSE). This can be changed by specifying na.rm = TRUE, such as shm(x, na.rm = TRUE).

Details

Harmonic mean is expressed as

$$\bar{x}_h = \frac{1}{\left(\frac{1}{n}\right)\left[\left(\frac{1}{x_1}\right) + \left(\frac{1}{x_2}\right) + \cdots + \left(\frac{1}{x_n}\right)\right]}$$

\(\bar{x}_h\)

the sample harmonic mean

x

the values in a sample

n

the number of values

"The harmonic mean is the reciprocal of the mean of the reciprocals. It is applied in situations where the reciprocal of a variable is averaged."

References

Nathabandu T. Kottegoda and Renzo Rosso, Statistics, Probability, and Reliability for Civil and Environmental Engineers, New York City, New York: The McGraw-Hill Companies, Inc., 1997, page 13.

See Also

mean for arithmetic mean

sgm for geometric mean, cv for coefficient of variation (CV), relerror for relative error, approxerror for approximate error, rms for root-mean-square (RMS), and ranges for sample range.

Examples

Run this code
# NOT RUN {
library("iemisc")
library("data.table")

# Example 1.12 from Kottegoda (page 13)
x <- c(0.20, 0.24, 0.16) # stream velocities in m/s
shm(x)

# using a matrix of the numeric vector x
mat1 <- matrix(data = x, nrow = length(x), ncol = 1, byrow = FALSE,
        dimnames = list(c(rep("", length(x))), "Velocities"))
shm(mat1)


# using a data.frame of the numeric vector x
df1 <- data.frame(x)
shm(df1)


# using a data.table of the numeric vector x
df2 <- data.table(x)
shm(df2)


# }

Run the code above in your browser using DataLab