diff --git a/NEWS.md b/NEWS.md index 3d9899578e..7a052b281d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ ### Enhancements * Added formatting function `format_count_fraction_lt10` for formatting `count_fraction` with special consideration when count is less than 10. +### Bug Fixes +* Fixed bug in `s_summary.logical` causing `count_fraction` values to display as `NA` instead of `0` in tables when denominator is zero. + # tern 0.9.1 ### New Features diff --git a/R/analyze_variables.R b/R/analyze_variables.R index 82086e7051..5621e9e19f 100644 --- a/R/analyze_variables.R +++ b/R/analyze_variables.R @@ -261,7 +261,8 @@ s_summary.numeric <- function(x, #' #' ## Basic usage: #' s_summary(factor(c("a", "a", "b", "c", "a"))) -#' # Empty factor returns NA-filled items. +#' +#' # Empty factor returns zero-filled items. #' s_summary(factor(levels = c("a", "b", "c"))) #' #' ## Management of NA values. @@ -382,6 +383,9 @@ s_summary.character <- function(x, #' ## Basic usage: #' s_summary(c(TRUE, FALSE, TRUE, TRUE)) #' +#' # Empty factor returns zero-filled items. +#' s_summary(as.logical(c())) +#' #' ## Management of NA values. #' x <- c(NA, TRUE, FALSE) #' s_summary(x, na.rm = TRUE) @@ -410,7 +414,7 @@ s_summary.logical <- function(x, N_col = .N_col ) y$count <- count - y$count_fraction <- c(count, ifelse(dn > 0, count / dn, NA)) + y$count_fraction <- c(count, ifelse(dn > 0, count / dn, 0)) y$n_blq <- 0L y } diff --git a/man/analyze_variables.Rd b/man/analyze_variables.Rd index 5fca62ec7c..cc4109fff1 100644 --- a/man/analyze_variables.Rd +++ b/man/analyze_variables.Rd @@ -335,7 +335,8 @@ lapply(X, function(x) s_summary(x$x)) ## Basic usage: s_summary(factor(c("a", "a", "b", "c", "a"))) -# Empty factor returns NA-filled items. + +# Empty factor returns zero-filled items. s_summary(factor(levels = c("a", "b", "c"))) ## Management of NA values. @@ -360,6 +361,9 @@ s_summary(c("a", "a", "b", "c", "a", ""), .var = "x", na.rm = FALSE, verbose = F ## Basic usage: s_summary(c(TRUE, FALSE, TRUE, TRUE)) +# Empty factor returns zero-filled items. +s_summary(as.logical(c())) + ## Management of NA values. x <- c(NA, TRUE, FALSE) s_summary(x, na.rm = TRUE) diff --git a/tests/testthat/_snaps/analyze_variables.md b/tests/testthat/_snaps/analyze_variables.md index 523e71cae7..64418235ed 100644 --- a/tests/testthat/_snaps/analyze_variables.md +++ b/tests/testthat/_snaps/analyze_variables.md @@ -866,6 +866,24 @@ [1] 0 +# s_summary works with length 0 logical vectors + + Code + res + Output + $n + [1] 0 + + $count + [1] 0 + + $count_fraction + [1] 0 0 + + $n_blq + [1] 0 + + # s_summary works with logical vectors and by default removes NA Code diff --git a/tests/testthat/test-analyze_variables.R b/tests/testthat/test-analyze_variables.R index 65fd718141..ef20482a95 100644 --- a/tests/testthat/test-analyze_variables.R +++ b/tests/testthat/test-analyze_variables.R @@ -127,6 +127,13 @@ testthat::test_that("s_summary works with logical vectors", { testthat::expect_snapshot(res) }) +testthat::test_that("s_summary works with length 0 logical vectors", { + result <- s_summary(as.logical(c())) + + res <- testthat::expect_silent(result) + testthat::expect_snapshot(res) +}) + testthat::test_that("s_summary works with logical vectors and by default removes NA", { x <- c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, NA, NA)