From d81550d0cccae824cc748de48e7fd50ea8d8033a Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 16 Apr 2020 18:02:18 +0200 Subject: Make na.rm = FALSE the default for geomean() This makes more sense and is in line with mean() from base R. Adapt tests and update docs. --- R/geomean.R | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'R') diff --git a/R/geomean.R b/R/geomean.R index 626829b..04328c8 100644 --- a/R/geomean.R +++ b/R/geomean.R @@ -1,4 +1,4 @@ -# Copyright (C) 2015 Johannes Ranke +# Copyright (C) 2015,2020 Johannes Ranke # Contact: jranke@uni-bremen.de # This file is part of the R package pfm @@ -19,8 +19,9 @@ #' #' Based on some posts in a thread on Stackoverflow #' \url{http://stackoverflow.com/questions/2602583/geometric-mean-is-there-a-built-in} -#' This function checks for negative values, removes NA values per default and -#' returns 0 if at least one element of the vector is 0. +#' This function returns NA if NA values are present and na.rm = FALSE +#' (default). If negative values are present, it gives an error message. +#' If at least one element of the vector is 0, it returns 0. #' #' @param x Vector of numbers #' @param na.rm Should NA values be omitted? @@ -31,8 +32,8 @@ #' geomean(c(1, 3, 9)) #' geomean(c(1, 3, NA, 9)) #' \dontrun{geomean(c(1, -3, 9)) # returns an error} -geomean = function(x, na.rm = TRUE){ - if (any(is.na(x)) & na.rm == FALSE) stop("Removal of NA values was not requested") +geomean = function(x, na.rm = FALSE) { + if (any(is.na(x)) & na.rm == FALSE) return(NA) if (any(x < 0, na.rm = na.rm)) stop("Only defined for positive numbers") exp(mean(log(x), na.rm = na.rm)) } -- cgit v1.2.1