From 0b8951af2b6de57f9036f51f21e6caf8c240165e Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 9 Aug 2023 20:20:52 +0200 Subject: [PATCH] no rounding by default --- R/mean_n.R | 22 +++++++++++++++------- man/mean_n.Rd | 5 +++-- tests/testthat/test-mean_n.R | 11 ++++++----- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/R/mean_n.R b/R/mean_n.R index 48db01f53..4b4f1a171 100644 --- a/R/mean_n.R +++ b/R/mean_n.R @@ -14,6 +14,7 @@ #' If a row's sum of valid values is less than `n`, `NA` will be returned. #' @param digits Numeric value indicating the number of decimal places to be #' used for rounding mean values. Negative values are allowed (see 'Details'). +#' By default, `digits = NULL` and no rounding is used. #' @param verbose Toggle warnings. #' #' @return A vector with row means for those rows with at least `n` valid values. @@ -54,7 +55,7 @@ #' mean_n(dat, 0.75) # 2 valid return values #' #' @export -mean_n <- function(data, n, digits = 2, verbose = TRUE) { +mean_n <- function(data, n, digits = NULL, verbose = TRUE) { # check if data is data frame or matrix if (!is.data.frame(data) && !is.matrix(data)) { insight::format_error("`data` must be a data frame or matrix.") @@ -65,6 +66,11 @@ mean_n <- function(data, n, digits = 2, verbose = TRUE) { data <- as.data.frame(data) } + # n must be a numeric, non-missing value + if (is.null(n) || all(is.na(n)) || !is.numeric(n) || length(n) > 1) { + insight::format_error("`n` must be a numeric value of length 1.") + } + # make sure we only have numeric values numeric_columns <- vapply(data, is.numeric, TRUE) if (!all(numeric_columns)) { @@ -79,11 +85,6 @@ mean_n <- function(data, n, digits = 2, verbose = TRUE) { insight::format_error("`data` must be a data frame with at least two columns.") } - # n must be a numeric, non-missing value - if (is.null(n) || all(is.na(n)) || !is.numeric(n) || length(n) > 1) { - insight::format_error("`n` must be a numeric value of length 1.") - } - # is 'n' indicating a proportion? decimals <- n %% 1 if (decimals != 0) { @@ -95,5 +96,12 @@ mean_n <- function(data, n, digits = 2, verbose = TRUE) { insight::format_error("`n` must be smaller or equal to number of columns in data frame.") } - round(apply(data, 1, function(x) ifelse(sum(!is.na(x)) >= n, mean(x, na.rm = TRUE), NA)), digits) + # row means + out <- apply(data, 1, function(x) ifelse(sum(!is.na(x)) >= n, mean(x, na.rm = TRUE), NA)) + + # round, if requested + if (!is.null(digits) && !all(is.na(digits))) { + out <- round(out, digits = digits) + } + out } diff --git a/man/mean_n.Rd b/man/mean_n.Rd index 24439e1f7..f2a04725a 100644 --- a/man/mean_n.Rd +++ b/man/mean_n.Rd @@ -4,7 +4,7 @@ \alias{mean_n} \title{Row means with minimum amount of valid values} \usage{ -mean_n(data, n, digits = 2, verbose = TRUE) +mean_n(data, n, digits = NULL, verbose = TRUE) } \arguments{ \item{data}{A data frame with at least two columns, where row means are applied.} @@ -20,7 +20,8 @@ row to calculate the row mean (see 'Details'). If a row's sum of valid values is less than \code{n}, \code{NA} will be returned.} \item{digits}{Numeric value indicating the number of decimal places to be -used for rounding mean values. Negative values are allowed (see 'Details').} +used for rounding mean values. Negative values are allowed (see 'Details'). +By default, \code{digits = NULL} and no rounding is used.} \item{verbose}{Toggle warnings.} } diff --git a/tests/testthat/test-mean_n.R b/tests/testthat/test-mean_n.R index e90d66b4c..08dc80b4a 100644 --- a/tests/testthat/test-mean_n.R +++ b/tests/testthat/test-mean_n.R @@ -6,11 +6,12 @@ test_that("mean_n", { c4 = c(2, 3, 7, 8) ) expect_equal(mean_n(d_mn, 4), c(NA, 2.75, NA, NA), tolerance = 1e-3) - expect_equal(mean_n(d_mn, 3), c(NA, 2.75, NA, 5.67), tolerance = 1e-3) - expect_equal(mean_n(d_mn, 2), c(1.5, 2.75, NA, 5.67), tolerance = 1e-3) - expect_equal(mean_n(d_mn, 1), c(1.5, 2.75, 7, 5.67), tolerance = 1e-3) - expect_equal(mean_n(d_mn, 0.5), c(1.5, 2.75, NA, 5.67), tolerance = 1e-3) - expect_equal(mean_n(d_mn, 0.75), c(NA, 2.75, NA, 5.67), tolerance = 1e-3) + expect_equal(mean_n(d_mn, 3), c(NA, 2.75, NA, 5.66667), tolerance = 1e-3) + expect_equal(mean_n(d_mn, 2), c(1.5, 2.75, NA, 5.66667), tolerance = 1e-3) + expect_equal(mean_n(d_mn, 1), c(1.5, 2.75, 7, 5.66667), tolerance = 1e-3) + expect_equal(mean_n(d_mn, 0.5), c(1.5, 2.75, NA, 5.66667), tolerance = 1e-3) + expect_equal(mean_n(d_mn, 0.75), c(NA, 2.75, NA, 5.66667), tolerance = 1e-3) + expect_equal(mean_n(d_mn, 2, digits = 1), c(1.5, 2.8, NA, 5.7), tolerance = 1e-1) }) test_that("mean_n, errors or messages", {