Skip to content

Commit

Permalink
Fix count_fraction return for empty logical vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua committed Oct 5, 2023
1 parent 4e76272 commit 41d9796
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions R/analyze_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion man/analyze_variables.Rd

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

18 changes: 18 additions & 0 deletions tests/testthat/_snaps/analyze_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-analyze_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 41d9796

Please sign in to comment.