Skip to content

Commit

Permalink
no rounding by default
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Aug 9, 2023
1 parent 89e834b commit 0b8951a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
22 changes: 15 additions & 7 deletions R/mean_n.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.")
Expand All @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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
}
5 changes: 3 additions & 2 deletions man/mean_n.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions tests/testthat/test-mean_n.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 0b8951a

Please sign in to comment.