diff --git a/NEWS.md b/NEWS.md index 81c2c1469f..861594981b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,9 +5,12 @@ * Refactored `summarize_vars` and `compare_vars` to use refactored `a_summary`. * Created new internal helper functions `ungroup_stats` to ungroup statistics calculated for factor variables, and `a_summary_internal` to perform calculations for `a_summary`. +### Bug Fixes +* Fixed bug in `s_count_occurrences_by_grade` so that "missing" grade always appears as the final level. +* Fix bug in `analyze_vars_in_cols` when categorical data was used. + ### Miscellaneous * Fix swapped descriptions for the `.N_row` and `.N_col` parameters. -* Fix bug in `analyze_vars_in_cols` when categorical data was used. * Removal of internal calls to `df_explicit_na`. Changes in `NA` values should happen externally to `tern` functions, depending on users' needs. * Reinstated correct soft deprecation for `create_afun_summary` and `create_afun_compare`. diff --git a/R/count_occurrences_by_grade.R b/R/count_occurrences_by_grade.R index 3ae250b66b..3ea79c806b 100644 --- a/R/count_occurrences_by_grade.R +++ b/R/count_occurrences_by_grade.R @@ -179,6 +179,10 @@ s_count_occurrences_by_grade <- function(df, grade <- formatters::with_label(factor(grade, levels = lvl_ord, ordered = TRUE), grade_lbl) } + missing_lvl <- grepl("missing", tolower(levels(grade))) + if (any(missing_lvl)) { + levels(grade) <- c(levels(grade)[!missing_lvl], levels(grade)[missing_lvl]) + } df_max <- stats::aggregate(grade ~ id, FUN = max, drop = FALSE) l_count <- as.list(table(df_max$grade)) } diff --git a/tests/testthat/_snaps/count_occurrences_by_grade.md b/tests/testthat/_snaps/count_occurrences_by_grade.md index 7952967649..ac93d474d5 100644 --- a/tests/testthat/_snaps/count_occurrences_by_grade.md +++ b/tests/testthat/_snaps/count_occurrences_by_grade.md @@ -101,6 +101,32 @@ +# s_count_occurrences_by_grade sorts grade levels so that 'missing' level appears last + + Code + res + Output + $count_fraction + $count_fraction$`1` + [1] 0 0 + + $count_fraction$`2` + [1] 2.0 0.2 + + $count_fraction$`3` + [1] 2.0 0.2 + + $count_fraction$`4` + [1] 2.0 0.2 + + $count_fraction$`5` + [1] 0 0 + + $count_fraction$Missing + [1] 0 0 + + + # s_count_occurrences_by_grade works with valid input for grade grouping Code diff --git a/tests/testthat/test-count_occurrences_by_grade.R b/tests/testthat/test-count_occurrences_by_grade.R index f6001186ab..20f8dbdd79 100644 --- a/tests/testthat/test-count_occurrences_by_grade.R +++ b/tests/testthat/test-count_occurrences_by_grade.R @@ -57,6 +57,16 @@ testthat::test_that("s_count_occurrences_by_grade works with valid input and def testthat::expect_snapshot(res) }) +testthat::test_that("s_count_occurrences_by_grade sorts grade levels so that 'missing' level appears last", { + df <- raw_data + df$AETOXGR <- factor(c("Missing", 2, 3, 1, 1, 2, 3), levels = c("Missing", 1:5)) + + result <- s_count_occurrences_by_grade(df = df, .var = "AETOXGR", .N_col = 10) + + res <- testthat::expect_silent(result) + testthat::expect_snapshot(res) +}) + testthat::test_that("s_count_occurrences_by_grade works with valid input for grade grouping", { df <- raw_data result <- s_count_occurrences_by_grade(