gmean | R Documentation |
Function to calculate the geometric mean analogously to R's built-in mean() function. It catches negative inputs, properly handles zeroes and allows for both NA removal and trimming (again, as in mean()) before processing. Adapted from Paul McMurdie's excellent example at https://stackoverflow.com/questions/2602583/geometric-mean-is-there-a-built-in.
gmean(x, trim = 0, na.rm = FALSE, zero.propagate = FALSE)
x |
An R object. Currently there are methods for numeric/logical vectors and date, date-time and time interval objects. Negative values will return NaN. |
trim |
the fraction (0 to 0.5) of observations to be trimmed from each end of x before the mean is computed. Values of trim outside that range are taken as the nearest endpoint. |
na.rm |
a logical value indicating whether NA values should be stripped before the computation proceeds. |
zero.propagate |
a logical value indicating whether inputs containing any 0 values should return 0 (zero.propagate == TRUE) or should calculate the geometric mean ignoring any 0 input values (zero.propagate == FALSE). |
x <- c(0:10, 50)
xgm <- gmean(x)
c(xgm, gmean(x, trim = 0.10))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.