Skip to content

Commit

Permalink
Correct ordering of grade levels in s_count_occurences_by_grade (#1040
Browse files Browse the repository at this point in the history
)

Closes #1018
  • Loading branch information
edelarua authored Aug 24, 2023
1 parent dcbf3e1 commit d9c524a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

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

0 comments on commit d9c524a

Please sign in to comment.