Skip to content

Commit d206945

Browse files
committed
added geometric mean
1 parent 2090925 commit d206945

10 files changed

+91
-5
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: esmisc
22
Type: Package
33
Title: Misc Functions of Eduard Szöcs
4-
Version: 0.0.3.9000
4+
Version: 0.0.3.9001
55
Date: 2017-01-10
66
Authors@R: c(person("Eduard", "Szöcs", role = c("aut", "cre"),
77
email = "[email protected]"))
@@ -23,4 +23,4 @@ Imports:
2323
readr
2424
Suggests:
2525
testthat
26-
RoxygenNote: 5.0.1
26+
RoxygenNote: 6.0.0

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(geomean)
34
export(read_regnie)
45
export(theme_edi)
56
import(ggplot2)

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ esmisc 0.0.4
44

55
NEW FEATURES
66

7+
* added function for geometric mean
8+
79
MINOR IMPROVEMENTS
810

911
BUG FIXES

R/geomean.R

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#' Geometric mean
2+
#'
3+
#' Function for the geometric mean.
4+
#'
5+
#' @details The geometric mean is comuted as
6+
#' \deqn{x = e^{(\sum \log x) / n}}
7+
#'
8+
#' @param x a numeric vector.
9+
#' @param na.rm a logical value indicating whether NA values should be
10+
#' stripped before the computation proceeds.
11+
#'
12+
#' @return numeric vector of length one with the geometric mean.
13+
#' @references \url{http://stackoverflow.com/questions/2602583/geometric-mean-is-there-a-built-in}
14+
#' @export
15+
#' @examples
16+
#' x <- c(1, 10, 100)
17+
#' mean(x)
18+
#' geomean(x)
19+
geomean = function(x, na.rm = TRUE){
20+
exp(sum(log(x[x > 0]), na.rm = na.rm) / length(x))
21+
}

README.Rmd

+10-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ p <- ggplot(mtcars) +
5555
facet_wrap(~am) +
5656
theme_edi()
5757
p
58-
````
58+
```
59+
60+
### other functions
61+
62+
#### Geometric mean
63+
```{r}
64+
mean(c(1, 10, 100))
65+
geomean(c(1, 10, 100))
66+
```
67+

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,15 @@ Examples
5555
p
5656

5757
![](README_files/figure-markdown_strict/ggplot_themes-1.png)
58+
59+
### other functions
60+
61+
#### Geometric mean
62+
63+
mean(c(1, 10, 100))
64+
65+
## [1] 37
66+
67+
geomean(c(1, 10, 100))
68+
69+
## [1] 10

man/geomean.Rd

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/read_regnie.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/theme_edi.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-geomean.R

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
context("geomean")
2+
3+
x <- c(1, 10, 100)
4+
gx <- geomean(x)
5+
6+
test_that('geomean works', {
7+
expect_is(gx, "numeric")
8+
expect_equal(length(gx), 1)
9+
expect_equal(gx, 10)
10+
})
11+

0 commit comments

Comments
 (0)